-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?

8
  • 1
    No. A Java file is uncompiled. You could create a shell or cmd file to run it. Commented Jul 2, 2017 at 3: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 + "\""}); Commented Jul 2, 2017 at 3:53
  • On what OS are you? This is OS-specific. Commented Jul 2, 2017 at 3: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. Commented Jul 2, 2017 at 6:35
  • @ErwinBolwidt windows (7), but hoping for a solution that would be able to be distributed to other os's and still work Commented Jul 2, 2017 at 6:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This seems like a good solution. Would it be possible for the user to pass in args doing it this way?
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 <path to your .jar file> 1 2 3
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.

7 Comments

This is just the process of creating a runnable jar, doesn't fix the problem
@Aequitas oh sorry, I misunderstand the problem, if you are in linux you can do what is following says askubuntu.com/questions/192914/…
@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.
@EJP 1. the title asks for "console" there's nothing about console here. 2. The question consists of more than just the title.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.