4

I have this java program that I want to package within a jar. It compiles fine and it compiles without errors into a jar. But when I double click it, it wont start. I know most jar applications uses JFrame and that works fine. But is it possible to make it command prompt/shell based? (Like Displaying System.out.println)

Example is it possible to execute this code by double clicking a jar:

public class Hello
{

  public static void main( String[] args )
  {
    System.out.println( "Hello, World!" );
  }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Dallox
  • 487
  • 5
  • 8
  • 19
  • 1
    I assume you have set your manifest correctly and running `jar -jar myjar.jar` on the command line works? – Peter Lawrey Apr 29 '12 at 16:07
  • I just exported the jar by using eclipse, and it wont work by that command. This is the jar: http://xedusphase.googlecode.com/files/BrOS.jar – Dallox Apr 29 '12 at 16:10
  • Yes, this code executes when you double-click on it. But you can't see it, because it's not in a terminal. Try `java -jar BrOS.jar`, although I'm not sure why Peter's command didn't work. –  Apr 29 '12 at 16:11
  • 1
    That actually answered my question. Thanks :D – Dallox Apr 29 '12 at 16:12
  • See also http://stackoverflow.com/questions/394616/running-jar-file-in-windows – Stephen C Apr 29 '12 at 16:14
  • @PeterLawrey: a) It is `java -jar yourjar.jar`, and the problem is something else: No shell, which can show the output. – user unknown Apr 29 '12 at 16:15

7 Answers7

10

There is no problem doing like that. But where do you expect to see the output?

If you execute from the console as java -jar my jar.jar you will see your "Hello, World".

If you want to double-click you'll need to create a JFrame.

Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
  • "If you want to double-click you'll need to create a JFrame" Thats kinda what i was looking for – Dallox Apr 29 '12 at 16:25
  • @Dallox If you want to double click you don't have to create a `JFrame`, take a look at my answer. – Jeffrey Apr 29 '12 at 16:27
5

You can change the file association for jar files to have them open a console. (This is for Windows)

  • Open Command Processor
  • Type ftype jarfile
  • You will get something like:

jarfile="C:\Path\To\Java\bin\javaw.exe" -jar "%1 %*"

  • Enter ftype jarfile "C:\Path\To\Java\bin\java.exe" -jar "%1" %* (You may need administrator privileges to do this).
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • It makes sense thats why i gave you a reputation point, but i kinda dont want the user to change the config of their PC to run the program. – Dallox Apr 29 '12 at 16:31
2

I don't see anyone mentioning the obvious solution: make a .cmd (or .bat) file containing the command everyone is talking about -- java -jar YourJar.jar. You can double-click on the .cmd file and a console window will open. It will also close imediately as your program exits, so the program should wait for a keypress before exiting.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

You can make a jar an executable by including a manifest file which contains the name of the class that has your main method (in your example that would be Hello). Here is a link that details what you need to do.

Raam
  • 10,296
  • 3
  • 26
  • 27
  • Whats the deal with the down vote. The question was "is it possible to execute this code by double clicking a jar" and that's what my answer talks about. Please add a comment as to why the downvote. – Raam Apr 29 '12 at 16:22
  • @Raam No, the question is how to get `System.out.println` to work from an executable jar file. The OP made his jar through Eclipse, it handles the manifest for him. While this information is technically correct, it does not answer the question. – Jeffrey Apr 29 '12 at 16:26
  • @Jeffrey, the info about eclipse was made in a comment, further the jar is being opened outside of eclipse by double clicking it. Hence my answer. Anyways it would be good if a comment justifying the downvote is made mandatory. – Raam Apr 29 '12 at 16:29
  • @Raam You cannot open a jar file in Eclipse, so I'm not sure what you're trying to say. Eclipse assumes that the user wants to execute the jar file outside of itself and creates the proper manifest automatically. (I am justifying my downvote, I don't feel that this answer is useful considering the context of the question.) – Jeffrey Apr 29 '12 at 16:34
  • @Jeffrey - What I meant by saying "the jar is being opened outside of eclipse" is that the user is free to change the JAR directly without any IDE being used. Again as I mentioned the eclipse info was not part of the question when I answered the question. In such circumstances I feel a comment questioning the answer is better, but that's me. I do see your point though. – Raam Apr 29 '12 at 16:40
  • Afaik, the Jar is executable, and the user knows how to make an executable jar. It does not open a terminal, where the message can be printed to. Maybe the downvoter thought that your answer is offtopic. – user unknown Apr 29 '12 at 19:24
0

Sure. First try to run your program as following:

java -cp yourjar.jar Main

where Main is a fully qualified class name of your main class.

When this is working fine and if you creted exacutable jar try to run your program as following:

java -jar yourjar.jar

When this is working double click should work too unless you mapped extension jar to program other than javaw that is done by default when you are installing JRE.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

If you want to display the "Hello, world!" String you have to execute your code from the command line:

   jar -jar your_jar_name.jar
imanis_tn
  • 1,150
  • 1
  • 12
  • 33
0

The output gets forwarded to the console but without eclipse, there isn't any! The only way to work around this is to launch the program in command prompt and then you will get the output from the program.