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.
Related
How can i export a jar file in eclipse with libraries? Executable jar wont work for me so that is out of the question. There is no option to do that when i try to export it..
If the jar is not running try to run it using a CLI for example command prompt java -jar <name>.jar and see the errors.
When you export the .jar there are 3 options .
One and two produces the .jar with the libraries inside.
Third option is producing a .jar (executable) with the libraries added
a different folder[ which has the same name as the jar file and it
must be named like it].
Mention that:
You may have serious problems and your executable will not run if your application has duplicate entries. So most of the time the third bullet is the best solution.
Sample image:
If you are trying to create a .jar to add it as a library to other projects then you should follow the below:
I want to make my project into a jar executable.
That much I can do but I need my external library as well.
How do you specify the location of an external library? Right now I have added it to my eclipse project,but later on I need it to execute my program outside of eclipse on a server.
The Library is the postgres-jdbc driver to connect to database.
Do you mean Fat Jar or Runnable Jar i.e a single jar which will contain all it's dependent jars in it.
If yes, it can be generated in eclipse using File -> Export -> Java -> Runnable Jar
Optionally you can specify the start-class having the main method.
If you don't want as a Runnable jar, you've to add all your dependent jars in classpath before starting your application.
java -cp <add all lib> -jar <jar-file-name>.jar
I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html
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
Well I have 2 .jar files. The main jar file is the jar file for my whole project and the other .jar file being the MySql JDBC Connector.
Well basically whats happening right now is that when I build the project I have the one main .jar file with everything but the MySql JDBC Connector .jar file is inside the main jar file when it builds in NetBeans.
Now when I am just running the project from within NetBeans the MySql JDBC driver can be found inside the src/com/game/mysql folder that I have it in. But when I build the project the Java application cannot locate the JDBC driver from within the main jar file.
When I open the main jar file with WinRar I can see that the JDBC jar file is still in its /com/game/mysql/ folder. But why cant the Java application access it?
I have heard that nested .jar files are not supported in Java so Im thinking this might be the reason although Im not sure if thats true. Is there a way that I can make it so that the application can find the JDBC .jar file within the main jar file?
Also I have done the thing in NetBeans where you add the .jar file through right clicking project -> properties -> Library -> Add Folder/Jar. Thats what makes it work in the NetBeans run but still not the App build.
I have heard that nested .jar files are not supported in Java
More precise, classes in a JAR file which is packaged as a child JAR inside a main JAR are indeed by default invisible to classes in the main JAR.
You have basically 2 options:
Ship your application with 2 loose JARs: your.jar and mysql.jar and define the relative path to the mysql.jar in the Class-Path entry of the MANIFEST.MF file of your.jar.
Class-Path: mysql.jar
When you put both JARs in the same folder and execute your.jar by java -jar your.jar, then it will work.
Let your IDE repackage all loose classes of mysql.jar inside your.jar or add a special classloader which preloads the classes of any embedded JARs. Since I don't do Netbeans, I can't tell whether it supports it and if so, how to do it. In Eclipse, however, this is definitely possible. See also this answer.