How do I create an executable java file?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
In BlueJ I create a .jar file with one class called test and the following code.

Code:
public class test{
public static void main(String[] args){
System.out.println("hello world");
}
}

However when I double click on the file nothing happens? :confused:
 
Last edited:
Isn't there a way just to have it execute by double clicking on it? It doesn't look very good making a program and then telling people that they have to execute it via the command prompt. It's currently set to open with "Java Platform SE binary".
 
Last edited:
It should run when you double click it if it runs fine from the command line. Your example won't do much though because it is a command line application and has no GUI so you won't see anything.
 
I think an executable jar has to be registered with the OS as well. Not the specific jar, but the file association for the OS to know to do a java -jar file in the background.

If you were actually distributing something, and you wanted to be sure the program would launch, i.e. to cut down the amount of kinks in the OS or the install of the JVM, you could use something like JSmooth: http://jsmooth.sourceforge.net/ or launch4j: http://launch4j.sourceforge.net/

Note though that the target machine still needs a JVM installed although I *think* launch4j can actually embed a JRE inside the exe.

P.S What RobH said about the "nothing happens", at best the command line will flash up for half a second, write the text and exit. I don't think it will even do that.
 
Last edited:
Thanks, I'll try those programs.

I would have thought the creators of java would have made it easier to create a distributable executable program.
 
Last edited:
P.S What RobH said about the "nothing happens", at best the command line will flash up for half a second, write the text and exit. I don't think it will even do that.

It won't, when executing a JAR by double-clicking it you don't see the output if the program has no GUI. The program will still execute but you won't see the output because there is no where to display it.

I would have thought the creators of java would have made it easier to create a distributable executable program.

That's what JAR's are for, as I said, your example program has no GUI so you won't see anything unless you execute it from the command line. The command line provides a console which you don't normally see when executing the JAR by double-clicking. The 'console' is not part of the application, the JVM provides it when running programs from the command line.
 
Last edited:
You can extend the JTextArea class and implement an out(String str) method which will append the text in the JTextArea. This will simulate the System.out call.

Then make a JFrame and add the extended JTextArea and call .out("hello world") as usual.

It will work, but only for output. Input you need to implement an in method and add a JTextBox and an "enter" JButton.

ps: this assume you understand swing and are somehow proficient with java. hope it helps.
 
Back
Top Bottom