i want to create a jar file of my java web application.
This web application has so many jar files related to this. so all am including in the build path. And when the time of jar file creation from eclipse,even if i had checked the checkbox for exporting the class files and resources option, the generated jar file doesn't have those library files. So when am trying to execute the jar file from command line option by using
java -jar myJarfile.jar
it will throw error like NoClassDefFoundError , can anyone help me to solve the issue.?
How can i generate the jar with all dependencies.?
You shouldn't really be including external dependencies in your jar. Just bundle up your code into your jar and specify the dependencies at runtime.
java -jar myJarfile.jar -cp someDependency.jar
Related
I am trying to create a jar using the
"jar cfe ..."
command.
However Iam unable to find a way to include all the dependent jar along with my build.Something like "Extract required libraries to generated jar" option in eclipse jar creation.
Please help if anyone knows how to do this in command line.
Edit:
I tried adding the jars as input-files ( docs.oracle.com/javase/tutorial/deployment/jar/build.html) but while running the jar i got ClassNotFoundException on the depended jars.
I also tried mentioning the jars in Classpath of manifest file,but that way i still have to put all the dependent jars in a file directory along with mg main jar.
I have created a jar file abc.jar. I want my jar to be converted to .exe file using the native-image command of graalvm. The jar needs some dependencies(external jars) to work.
So my command looks like:-
native-image -jar abc.jar output -cp "abc.jar;C:\lib\*"
The exe is generated and works on my machine. Nevertheless, when I ship it to a different matching and try to run the same it doesn't work unless I place all libs under C:\lib folder(the very same folder where I placed the external dependent jars). How can I create an executable with the jars included as part of exe file.
I tried the following specified in the documentation and no luck.
Regards.
I have a java desktop application in netbeans. I have created an executable jar file for the project using clean and build command provided by the netbeans. By using this command the executable jar file gets created under netbeansProjects//dist/.jar. I am able to execute this jar file from command line using java -jar .jar from within project path. But the problem is that when i move this jar outside of netbeans projects folder, say to desktop and run the jar file, it is giving error of type "Exception in thread "main" java.lang.NoClassDefFoundError". How to solve this problem and make the jar file executable from any location of the system.
Complete instructions may be found in dist/README.TXT:
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Ensure that the manifest inside of the jar file contains the necessary classpaths. If you are unfamiliar with the concept, go here: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Netbeans has probably included any external projects/libraries/Jars in the dist/lib folder.
In order to run the application, you must include all the files in the dist folder when you copy the application
Check if in your projects Manifest.mf file has the Attribute
"Main-Class" set to your projects current main Class file.
I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.
But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]
It can generate jar. But the jar can only be executed in the directory where it is produced.
If I put it into another directory or machine, it complains NoClassDefFoundError.
So, my question is that how I can generate executable jar using command line as Eclipse.
A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to
1) Create a manifest file which lists all the dependency jars
Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
dependency4.jar dependency5.jar
2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.
3) In this same folder where you created your jar, place all the dependency jars.
Now your folder will look like this,
yourjar.jar (With the manifest file you created above)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar
4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.
Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :
so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.
My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post
I have developed an application using Eclipse IDE. After creating the application I exported the project in jar format. When I am trying to run this jar file, I get the error: Unable to load main class. Please Help.
When you are exporting your project as a jar (see this SO question), you must specify your main class in the export Jar wizard.
This should work always:
java -cp MyJar.jar pkg.name.MyClass
I'd prefer this anyway because it causes less classpath trouble compared to the java -jar way of starting a java application.
You need to create a runnable jar file. From Eclipse 3.4 you can do that by choosing Export, Java, Runnable Jar File. If you also want to include dependencies have a look at the Fat Jar plug-in.