-2

I've created a runnable jar file from my application but when I open it, it appears to do nothing. My guess is that this is because my application does some internal calculations and then just uses System.out to print some messages to the user (and take input too). So even if the program opens there is no console to print to so it appears nothing is happening.

If I run the jar file through the console java -jar myjar.jar then it works as intended.

Is there any way for it to open a console and run through that when I double click the jar file?

dimo414
  • 47,227
  • 18
  • 148
  • 244
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • 1
    No. A Java file is uncompiled. You could create a shell or cmd file to run it. – Elliott Frisch Jul 02 '17 at 03:44
  • You can use something like, String filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6); Runtime.getRuntime().exec(new String[]{"bash", "java -jar /"" + filename + "\""}); – abstractnature Jul 02 '17 at 03:53
  • On what OS are you? This is OS-specific. – Erwin Bolwidt Jul 02 '17 at 03:59
  • Your title doesn't agree with your question. If `java -jar` works, you have already created the JAR file correctly, and that is what you should double-click, not the Java file. Clarification required. – user207421 Jul 02 '17 at 06:35
  • @ErwinBolwidt windows (7), but hoping for a solution that would be able to be distributed to other os's and still work – Aequitas Jul 02 '17 at 06:54
  • @EJP as I said in the question, double clicking the executable jar file does nothing but if I run it through the console with the `java -jar ` command then it works – Aequitas Jul 02 '17 at 06:55
  • The highest voted answer on this question (not the accepted one) provides a good solution: [Running a .jar file in a command prompt from double click](https://stackoverflow.com/questions/16543234/running-a-jar-file-in-a-command-prompt-from-double-click) – Erwin Bolwidt Jul 02 '17 at 08:31
  • Thanks @ErwinBolwidt I didn't manage to find that as I was looking for an answer. Currently its the same as Omrisk's answer here. I'll mark it as duplicate but it would still be good to be able to pass arguments in. – Aequitas Jul 02 '17 at 08:34

2 Answers2

1

If you are using an ide as Eclipse, you could do:

Select Java -> Runnable JAR file -> Next.

Select the Launch Configuration and choose project file as your Main class

Select the Destination folder where you would like to save it and click Finish.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • This is just the process of creating a runnable jar, doesn't fix the problem – Aequitas Jul 02 '17 at 03:39
  • @Aequitas oh sorry, I misunderstand the problem, if you are in linux you can do what is following says https://askubuntu.com/questions/192914/how-run-a-jar-file-with-a-double-click – developer_hatch Jul 02 '17 at 04:46
  • @Aequitas It is, but that is exactly what is asked for in the title of the question. If it's not what the OP wants it is up to him to clarify. – user207421 Jul 02 '17 at 06:34
  • @EJP 1. the title asks for "console" there's nothing about console here. 2. The question consists of more than just the title. – Aequitas Jul 02 '17 at 07:00
  • @DamianLattenero Thanks for that, unfortunately that's a client side solution. I would like it to work to anyone I give it to by just double clicking. – Aequitas Jul 02 '17 at 07:01
  • @Aequitas The title asks how to construct the JAR file, and this question answers it. As it happens that's not the actual question, but that's the OP's fault. `java -jar` is a 'console' command. Your point escapes me. – user207421 Jul 02 '17 at 08:04
  • @EJP please try and read more than just the title of the question. From the question body: (The part just below the title which you seemed to have missed) "I've created a runnable jar file..." So I've already created the runnable jar file so obviously don't need help making it. if you continue reading from that point you'll read why this doesn't solve my problem and what I wish to accomplish. – Aequitas Jul 02 '17 at 08:09
1

What you could do is create a .bat file to run the jar. Create a file called launch.bat and open it with any text editor and add the following text:

 java -jar <path to your .jar file>

then save and close. If you double click the launch.bat it will open and run the jar.

If you want the console to remain open then you can add

Scanner scanner = new Scanner(System.in); 
scanner.nextLine();

at the end of your main function to stop the console widow from closing.

Omrisk
  • 151
  • 9
  • This seems like a good solution. Would it be possible for the user to pass in args doing it this way? – Aequitas Jul 02 '17 at 07:23
  • Yes, if you add the arguments after the .jar file name you can write arguments and access them through the `String[] args` array in your main function. For example: `java -jar 1 2 3` – Omrisk Jul 02 '17 at 11:48