Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.println in jar

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!" );
  }
}
like image 600
Dallox Avatar asked Dec 03 '22 04:12

Dallox


2 Answers

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.

like image 165
Carlos Tasada Avatar answered Dec 09 '22 15:12

Carlos Tasada


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).
like image 39
Jeffrey Avatar answered Dec 09 '22 15:12

Jeffrey