Debugging in Java is fairly easy. Popular IDEs provide great debugging tools for Java developers. For example, I love the “Evaluate Expression” feature which is available in IntelliJ IDEA.
Recently, I had the following problem: I have a JavaFX application which ran fine when running through the IDE. However, when I created a JAR file and tried to run it, then no window showed up. It looked like some issue in the code prevented the application from working when running as a JAR file. Therefore, I needed to debug the JAR file which I had never done before.
In this article, I will show you how to debug a JAR. Of course, you need to have Java installed to run a JAR.
How to run a JAR from a terminal/cmd
If you do not need to debug with breakpoints but still want to inspect the logs, you can do the following:
- Create the JAR if you do not have it yet (either by using your IDE or by command-line).
- Open your terminal or cmd and navigate to the path of the JAR.
- Run the following command:
java -jar YOUR_JAR.jar
Now, if there is an issue which makes your application crash at the start, you will see the logs in your terminal. In my case, there was a NullPointerException when I tried to access an image which did not occur when running from the IDE.
If you want to run another class inside your JAR which has a main method, then use the following command to execute it:
java -cp MyJar.jar com.mycomp.myproj.AnotherClassWithMainMethod
How to debug a JAR in your IDE
If the way above is not enough for you then you can debug in your favorite IDE as well. In the following, I will be using IntelliJ IDEA (my favorite IDE) but you can use any IDE you want (e.g. Eclipse, Netbeans).
- Create the JAR if you do not have it yet.
- Add a new Run Configuration: choose JAR application, specify the path to the JAR you want to run and provide the following VM options:
3. Debug using the created run configuration. Now, you can use the debugging tools of your IDE to the fullest (e.g. set breakpoints, evaluate expressions).
Thanks for reading this article about debugging a JAR file. Let me know in the comments if it helped you.