NetBeans-made jar file won't work - java

So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?

The things that seem to be needed in NetBeans are:
The project has to be the Main Project (by right clicking on it in the Projects list).
You have to set the Main Class in the project properties. (Right click, Properties, Run, Main Class.)
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist subdirectory.
If that fails to fix the problem, here's a longer story...
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
java -jar filename.jar
(When you say it works for you from the command line, is this what you're typing?)
At that point, the Java executable looks for a file inside the jar named META-INF/MANIFEST.MF. And then in the contents of that file, it looks for the value of a property, Main-Class. And finally it looks for the class of that name in your jar and runs its static main(String[]) method.
So if your jar is failing to run, you can do the following to debug what's going on:
Clean and rebuild your project in NetBeans.
Double check that your class(es) are actually in the jar:
Start a command prompt
cd into the dist subdirectory of your project.
Use a command like jar tf filename.jar to list what's in there.
Double check that the MANIFEST.MF file is correct:
Again in a command prompt
cd into the dist directory.
Use a command like jar xf filename.jar META-INF/MANIFEST.MF to extract the manifest.
Look at the contents of that file (e.g. type META-INF\MANIFEST.MF) and make sure Main-Class is set to the appropriate class.
If all of the above check out, then double clicking the file should work.

Have you set the containing project as the "Main Project"?

Related

Unable to run JAR file outside Intellij

I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version

Java11/JavaFX11/Module Deployment Mess (Runnable Jar that works if clicked, but doesn't if ran from command line)

I'm new to Java11/all the overcomplicated module stuff.
The Problem
So I exported my Java11/JavaFX11 program from Eclipse as a Runnable JAR. If I click the JAR, it runs perfectly fine (Eclipse includes all of the module settings and JavaFX itself automatically in the runnable JAR). However, if I try to bundle the JAR with a JRE and run it via the command line with the following BAT file:
#ECHO OFF
%~dp0\jre\bin\java -jar javaprogram.jar
pause
I get:
Error: JavaFX runtime components are missing, and are required to run this application
Press any key to continue . . .
How can I get it to just run the JAR file like it does when I click it?
Ways I've tried to fix it
The weirdest part is, if I just use:
java -jar javaprogram.jar
Which just accesses the installed JRE, it works again. It's only when I'm directly pointing it to a JRE at a specific path that it appears to break.
Alternatively, I'd just bundle JavaFX beside the JRE, but there doesn't seem to be a way to call --module-path with a relative path (googling this nets me a bunch of entirely unrelated stuff). It seems to demand an exact path, which isn't going to work if people are downloading a zip archive and extracting it. This would be redundant though because Eclipse is already packaging JavaFX with the JAR. I don't know why it's getting confused just because I'm calling it from the command line.
The project's code
The project I'm trying to get this to work with happens to be open source, so you can check out the code for it here:
https://github.com/SkyAphid/JDialogue
The main class is JDialogueCore.
Closing
I don't want to use installers since I think that's too bloaty. I'd like to be able to deploy my software like I always have by just putting them in an archive you can extract and run.
It's difficult to simply Google the problems as well since I keep getting completely unrelated results due to the broadness of the topic. Any direction/documentation relating to this problem would be greatly appreciated.
Thank you for your time!
While Java 8, 9, and 10 allowed a JavaFX Application subclass to act as a main class for program startup, that is no longer the case as of Java 11. Placing your public static void main method in a different class and making that class the main class solves the problem. (Source: https://github.com/javafxports/openjdk-jfx/issues/236)
Your command line invocation needs to specify both the location of the JavaFX jar files, and the location of JavaFX native libraries. Normally these are the same location in the JavaFX SDK, but they must be specified in different ways: the jar files go in the classpath or module path, while the native libraries’ location must be specified in a system property:
cd /d %~dp0
jre\bin\java -cp javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib com.example.MyNonApplicationClass
If you define a module-info.java in your program, your .jar is a modular .jar, and you can benefit from the additional security of modules:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp/com.example.MyNonApplicationClass
If your modular .jar file has a main class defined, you can omit the class name:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp
Notice that relative paths work just fine with --module-path. Relative paths use the current directory as a base. The current directory is not changed merely by putting %~dp0 in front of the invocation of Java. The current directory is a property of the command line or script actively running, and can only be changed with commands like cd or pushd.

Creation of a .bat file of a java application

I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.
.BAT file code
javac Application.java
java -cp . Application
Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.
Thanks ,
The first line in your batch file is attempting to compile your program !?
The second line is attempting to run the Application.class file.
What you want if you have produced an executable jar file is:
java -jar Application.jar
But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.
Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.
In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file
java -cp <the path to class file> Application
So you might be using a javac just to take advantage of class path as current directory. So when you say
javac Application.java
java -cp . Application
It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.
javac Application.java
java Application
I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.
In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.

Java application will run from CMD and Eclipse but not double click

I've done a search and I can see that a lot of people have had the same problem as me, but none of the solutions have worked for me.
Basically I have a Java Project in Eclipse that is from my old Windows Installation. I've cleaned and rebuilt it because at first it wouldn't compile, but now I have it exported as a Runnable Jar. However, the only way I can get the application to appear is to do java -jar foo.jar in command prompt, or run it in Eclipse. If I double click the JAR in Windows Explorer nothing happens even though I know that Java is associated correctly because other Runnable Jars work.
The project only has the x86 JRE listed in it's Build Path Libraries and all the files listed appear to exist. I'm running Windows 7 HP.
Update: I'm sorry, but I just discovered that no other Runnable Jars are working either. If they are wrapped with launch4j they work though...
Edit: The Runnable Jars that I export from Eclipse do work fine on other systems and load on double click
Some registry values or file associations are probably messed up. Wiping off all of your existing JRE's and JDK's and re-installing them should fix your issue.
Alternatively you may be able to fix it by manually editing the registry value here:
HKLM > SOFTWARE > Classes > jarfile > shell > open > command
My value is
Type: REG_SZ
Data: "C:\Program Files\Java\jre8\bin\javaw.exe" -jar "%1" %*
You'd of course want that path to point to your javaw.exe, and make sure you have the additional arguments.
I was also facing the same problem while i was working with Spring tool suite.
You may use the following steps:-
Right click on project -> export -> Runnable jar file -> (Here,In library handling,there are three options,you have to choose middle one i.e package required library into generated jar.It will package external dependency also).
-In my case, my runnable jar was only executing on my environment i.e on which i have created that JAR. Initially i have selected the first option to create JAR i.e extract required libraries into required JAR.but that was not proper.
It may help you.Let me correct if i am getting wrong.
Since you are able to run the JAR running the command line, I believe your issue is related to which version of Java is set to run the file when it is double-clicked.
To find out which version is successfully running the file from the command line and set it to open JAR files by default:
Open a new command prompt window.
Run echo %path%. Among the path values, you should be able to find one pointing to the bin folder of one of the installed versions of Java. Copy this path somewhere.
Navigate to the JAR file you would like to run. Right click the JAR -> Open with -> Choose default program... -> Browse...
Browse to the path you copied in step 2. (the easiest way is to paste it into the address bar)
Double click javaw.exe.
Click OK.
You should now be able to run the JAR file. Please let me know if your problem persists.
I have made a jar with and keep it on desktop.Then,I double clicked on the jar and it working fine for me.
How I and what I have monintored: In my main class, perform some operation and at the end I add on Thread.sleep(25000); to hold the program for few moments. After every double click on the exported jar I found one new javaw.exe process added in the system process tree. I have noticed it on Task manager. and after 25000ms respective javaw.exe process ended. As my application does not cointain any GUI that's why I have not seen any GUI changes for the respective process. I'm Confident that if my application have some GUI, I will surely get the respective GUI window on every run.
Common mistakes : when we export a project from Eclipse as Runnable JAR file, it is exported with selected Eclipse launch configuration and Eclipse specific launch wrappers. Now if the configuration does not match when you are trying to run it via double-click you will not be able to see the error, If you run it from CMD then surely you will get the error log.
To create standard executable JAR file : To create a standard executable JAR file, you can export as JAR file and specify the main class in last screen of the wizard.
That's working for me fine. I used the same jar from different system and keeping it different location.

Cannot run my .jar file

I tried double clicking it, I didn't get anything, I tried opening it in CMD, I got this:
no main manifest attribute, in MyRestaurant.jar
What should I do ? :(
When you create a JAR you need to specify which class has the main method you want to run, this is done by adding a manifest.txt as a file to the jar you create. It seems thats the thing its missing.
Create a manifest.txt file, just add one line Main-Class: ClassName followed by enter and add that file when you create your jar.
ex. -jar -cvmf manifest.txt app.jar ClassName.class
If you are using Netbeans IDE, the first time you complied, choose Run then Netbeans will ask you the main class. From next time, you can just Clean and Build.

Categories