views:

1926

answers:

9

I'm creating a Java application that will do some processing then needs to display a message to give the user feedback.

However, it appears to be incredibly slow - taking over two seconds to return.

I stripped the source down to the apparent culprit, and here is the code used:

package SwingPlay;

import javax.swing.JFrame;

public class Dialog
{

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( "DialogDemo" );
    }

}

I'm executing this from the command line with:

java -classpath . SwingPlay.Dialog

As you can see - I'm doing nothing but create a JFrame, not even displaying it.

In case it is relevant, here is my java -version output:

java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

And this is (currently) running against Win XP SP2.


So, first question: Why is it so slow?

More importantly, I just want a simple message (GUI, not cmdline) to be displayed without delay - can anyone provide some code to do this?


Update:

A bit of background might be helpful:
I am creating an application which will have many 'heads' (i.e. different user interfaces all using the same core classes to do the complex parts).
I currently have a pure command line head which works fine - responds straight away.
I will also have a standard application with a regular point & click GUI, and don't foresee problems with this bit.
What I am currently working on is a hybrid of these two - it will be launched from a Run box (or similar launcher), possibly with arguments, and only needs to respond with, effectively, a status message, that can be dismissed with a key press.

This latter one is where the question is focused.

Whilst I am not opposed to using my existing command line version with shell scripts (though didn't think it would be necessary!), the existing answers seem to suggest that things are not running as fast for me as they are for others - one example takes 1460ms for me, versus 70ms - a significant difference.

A: 

What you're probably looking for is the new SplashScreen functionality in Java 6. Instead of having to wait for the JVM to load (there's always a cost to load any VM), this will load a screen beforehand.

Stephane Grenier
I'm not looking to display a static image, but a dynamic piece of text.To put it another way, I want to redirect stdout to a GUI message box.If I do actually use System.out.println it comes back quickly enough, but obviously not in the correct format.
Peter Boughton
+1  A: 

Also it would be a lot faster to create an AWT Window (or maybe a Frame) instead of a JFrame because the latter has to pull in a gazillion of additional class files.

Bombe
Whilst java.awt.Frame is faster, it's still over a second of delay. :(
Peter Boughton
Yes, unfortunately it isn’t really fast. If you have everything in a single program, create the frame before performing the real action and then simply show it when you’re done. It won’t be faster overall but it will certainly look that way.
Bombe
The real action happens in a blink, so unfortunately I wont even gain that illusion...
Peter Boughton
+1  A: 

Oh, and if you don’t really need to show the dialog from Java you could look into using KDialog (or it’s GNOME counterpart) or something similar.

Bombe
The core of the application needs to be Java, but if using even a basic interface is too slow, then I can make do with using shell scripts to display the response from the program.
Peter Boughton
Yes, the first instantiation of something GUI-related in Java will always be slow(er), there’s no way to prevent that, short of going native (either via JNI or with Runtime.exec()).
Bombe
+1  A: 

I would use a JOptionPane to show the message. Here's a simple example:

import javax.swing.*;

public class OptionDemo {
    public static void main(String[] args) throws Exception {
        JOptionPane.showMessageDialog(null, "Hello World");
    }
}

I'm afraid I can't explain the delay you're experiencing though. On my system, your code snippet runs in 500 milliseconds.

Jason Day
This seems roughly the same speed as the AWT example I tried (over a second, maybe around 1.5s), which is an improvement, but still annoyingly slow. If I could get it to 500ms that might be acceptable.
Peter Boughton
A: 

You could use the JOptionDialog

JOptionPane.showMessageDialog([parent frame], [message], [title], JOptionPane.MESSAGE_TYPE);
ShawnD
+2  A: 

Java is the wrong tool for this. Setting up the JVM involves a lot of stuff happening in the background before the first line of Java code can be executed, and there's really no way to get around it.

Michael Borgwardt
If the JVM is the cause, why is it not slow when outputting to console, via System.out.println('Text') ?
Peter Boughton
@Peter: Because the JVM does not initialise the AWT sub-system if it is not needed. It's most likely that that is causing the delay in this case.
Dan Dyer
+2  A: 

The reason for the delay it because Java is an interpreted language and it takes time to start a new JVM ( the interpreter )

Actually creating the frame takes less than a few ms ( about 70 ms in my machine ).

If this is going to be used within a Java app, you don't need to worry about it. It will be almost instantaneous ( you should use JDialog or JOptionPane for this )

If this is NOT going to be used inside a Java app, and 2 secs it too much ( and I think it is too much ) you should consider another tool for the job.

Here's how I measure the time in your code:

import javax.swing.JFrame;

public class Dialog {

    public static void main( String[] args ) {
        long start = System.currentTimeMillis();
        JFrame frame = new JFrame( "DialogDemo" );
        System.out.println( "Took: " + (  System.currentTimeMillis() - start   ) );
    }

}
OscarRyz
Hmmm, using your code takes an average of 1460 ms here - that is much slower than your 70ms - so there must be something wrong with my setup?This specific bit of code could use a shell script, but I would still like to find out why I am getting such bad performance compared to you/others.
Peter Boughton
Takes about 400 to 500 ms on my machine (4-year old Pentium 3 M with 1.8 GHz).
Bombe
The 70 ms are elapse between the "long start ..." to the System.out... ( exactly as printed by the test ) From the console it takes about 1.5 secs. but again, that's the JVM startup. From within a Java app you don't get that 1.5 extra.
OscarRyz
A: 

Have you tried running it through a profiler like NetBeans? If there's a bottleneck deep inside the standard library, that's a good way to find it.

Michael Myers
+2  A: 

Do you NEED to use java to display the message box? IF the box is coming from outside of your application, then you might want to use something else to generate a dialog.

To make a native windows app that just shows a message box from a command line string would only take a few hours at most. Most of the common scripting languages should have ways to do it too. here's an example from some guy through javascript via command line:

http://www.snee.com/bobdc.blog/2009/01/displaying-a-message-box-from.html

John Gardner