I have tried to create Jar file using Command Line.
My Manifest file:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class:Home
My Files and Location of that files:
Location : D:\Application
Files :
images
add.png
home.png
minus.png
Database.java
Home.java
UiDesign.java
Database.class
Home.class// This is my main class
UiDesign.class
Manifest.txt
mysql-connector-java-5.1.15-bin.jar
To create jar file i tried:
D:\Application>jar cmf Manifest.txt MyApp.jar *.class mysql-connector-java-5.1.15-bin.jar images
But the jar file is created. If i click that jar file, Error message shows like below,
Failed to Load Main-Class manifest attribute from
D:\Application\MyApp.jar
Thank for all....My problem solved by adding new line in Manifest File....Thank you all...
But now i have another problem.....
D:\JavaApplication-13-8-2011\Application>jar cfm MyApp.jar Manifest.txt *.class
mysql-connector-java-5.1.15-bin.jar images
D:\JavaApplication-13-8-2011\Application>java -jar MyApp.jar
Connect to MySQl
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database.getDBConnection(Database.java:14)
at UiDesign.<init>(UiDesign.java:58)
at Home.main(Home.java:6)
java.lang.NullPointerException
at Database.getBrand(Database.java:31)
at UiDesign.<init>(UiDesign.java:59)
at Home.main(Home.java:6)
Exception in thread "main" java.lang.NullPointerException
at UiDesign.<init>(UiDesign.java:64)
at Home.main(Home.java:6)
D:\JavaApplication-13-8-2011\Application>
I think this problem occurs due to to class path not set to mysql-connector....I have added this jar file in my Application.....How to setclass path and run my Application jar file successfully.....
please help me....
Thank you...I cleared..this problem also...As Trisstan said, I added classpath in Manifest file...Now My Application Jar file Run Succesfully.....
Thank You all for your Quick Response......
Add a newline at the end of your manifest file.
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
From: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
It may be an error about classpath, because your jar depends on mysql-connector-java-5.1.15-bin.jar
make sure there is a carriage return in your manifest file and I am also guessing your jar file name should precede the manifest file as your option says cfm.
Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar
From the error it looks like it thinks that the jar file is your manifest file. That should be the problem.
update the argument or change the option parameter to mcf I guess..
For your second problem:
Your jar probably contains a copy of the mysql jar. That does not work because jars are supposed to contain classes. The simplest way to fix this is to indicate in your manifest that your code depends on the mysql jar. Basically add the following:
Class-Path: mysql-connector-java-5.1.15-bin.jar
In your jar creation command, do not include the mysql jar. Finally, make sure that your jar and the mysql one are distributed together (in the same directory).
If you really want a single jar, extract the mysql jar and include its contents in the jar you are going to distribute.
Related
I referenced one external library (Opencsv) in my project.
I added the Manifest file into the 'src' folder as:
Manifest-Version: 1.0
Main-Class: DataClean.DataPreprocess
Class-path: jars/opencsv.jar
The 'jars' is a folder contains the opencsv jar file in 'src' folder.
The complied main class is in 'DataClean' folder in 'src'.
When I create the jar, I use:
jar cfm PromoPrice_CN.jar Manifest.txt *
This command was run within 'src' folder.
Then, the JAR I created can only run within this 'src' directory. Otherwise, it gives the exceptions:
Exception in thread "main" java.lang.NoClassDefFoundError: com/opencsv/CSVReader
at DataClean.DataPreprocess.dataClean(DataPreprocess.java:154)
at DataClean.DataPreprocess.main(DataPreprocess.java:229)
Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVReader
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
The command I used to run the jar is:
java -jar C:\...\src\PromoPrice_CN.jar X Y Z
X, Y and Z are three input parameter for the jar.
Please help where did I do wrong?
FYI, the creation of the JAR has to be in command line for other reasons.
Thx
I'm trying to create a jar file for one of my Java files. However, once I create it and try to run it, I get a java.lang.NoClassDefFoundError.
I did the following to create the jar. The file is Worker.java and it's in src/beatDB.
jar cfm Worker.jar Manifest.txt src/beatDB
My Manifest.txt file is just
Main-Class: Main-Class: beatDB.Worker \n (with a new line afterwords)
When I run java -jar Worker.jar, I get this error.
Exception in thread "main" java.lang.NoClassDefFoundError:
beatDB/Worker
Caused by: java.lang.ClassNotFoundException: beatDB.Worker
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
However if I run java beatDB/Worker, the file runs fine.
Does anyone know how to fix this problem?
You should jar the manifest along with the .class files, not the .java files.
.java files contain the source code.
.class files contain the compiled code. For Java it is called byte code.
You want the package structure of your classes to be reflected in your jar file. If you have an Animal class under com.example, then your jar file should contain com/example/Animal.class
See How to create jar file with package structure? for the proper jar file package structure.
I use tess4j library for my java app. I followed instructions and copied liblept168.dll and libtesseract302.dll into my project root folder. When I run my app with Eclipse, it works normally. But when I export it as runnable jar file, it gives me java.lang.NoClassDefFoundError - UnsatisfiedLinkError: The specified module could not be found. But I am sure, that program see this files, because earlier before I added this files it gave me other error, that files can't be found. I also used to copy this dlls to my jar archive, but it didn't help. I am absolutely sure, that I use right jre too. I just can't understand what can be wrong...
Tesseract1 api = new Tesseract1();
api.setTessVariable("tessedit_char_whitelist", "(),-+0123456789");
//part of code of getting image
String result = api.doOCR(image);
Any ideas?
UPD:
I edited my manifest file as you said and it is:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jsoup-1.7.3.jar json_simple-1.1.jar Filters.jar ja
i_core.jar jna-4.1.0.jar tess4j.jar jai_codec.jar jxl.jar myjsocksj.j
ar jai_imageio.jar
Class-Path: jai_imageio.jar tess4j.jar
Rsrc-Main-Class: com.slando.MainWindow
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Then there were errors about missing dll files and I copied them into jar. And then the same error began to occure.
Stacktrace:
java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.Tesseract1
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)
One more:
java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.Native.open(Native.java:1759)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Native.register(Native.java:1396)
at com.sun.jna.Native.register(Native.java:1156)
at net.sourceforge.tess4j.TessAPI1.<clinit>(Unknown Source)
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)
Eclipse has the tess4j jar in its classpath, but your system does not. To distribute this project, the tess4j jar needs to be in the classpath in your jar manifest. You can also add the path to this jar to the classpath environment variable in your system settings.
Someone else had the same problem
I've been building an application in Eclipse which extracts data from salesforce and using an imported CSV file, updates and inserts records. It works fine from within Eclipse and I'm happy it's ready for deployment, but I cannot get the application working if I try and run it from cmd or double click the Jar. I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: uploader/jar
Caused by: java.lang.ClassNotFoundException: uploader.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: uploader.jar. Program will exit.
I've been all over google and tried every solution out there, but can't find why it won't work. Here's the steps I'm using to build the jar and run it, please if anyone can show me where I'm going wrong, I'd be delighted! I'm not a java programmer...
The project is called uploader as is the package, by the way
Right click on project and click Export. Choose "Jar file" and
Next.
Select my project as the resource and ensure .classpath and .project
files are selected. Choose to Export generated class files and
resources, to Compress the contents of the JAR file, and choose the
location for the Jar file to go, then hit Next.
Choose to Export class files with compile errors, and warnings.
Choose to Save the description of this JAR in the workspace, and
give the location as /uploader/jardesc.jardesc. Hit Next.
Choose to Generate the manifest file, Save the manifest in the
workspace and Use the saved manifest in the generated JAR
description file.
Point the Main Class to the correct place by clicking Browse and
choosing the only option that I'm given, which is uploader.Main,
then click Finish.
If I now open the manifest file, I see the following:
Manifest-Version: 1.0
Main-Class: uploader.Main
So that looks correct to me.
However, if I open a command prompt, cd to the directory containing the jar file then say java uploader.jar, that's when I get the error above.
Can anyone see anything that looks wrong? I can't get past this point!
If I run with java -jar uploader.jar, I get this error instead:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sforce/ws/ConnectionException
Caused by: java.lang.ClassNotFoundException: com.sforce.ws.ConnectionException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: uploader.Main. Program will exit.
Perhaps that can help pinpoint the problem?
*EDIT*
I've edited the manifest.mf file to this, but I'm still getting the error:
Manifest-Version: 1.0
Main-Class: uploader.Main
Class-Path: lib/enterprise.jar lib/wsc-22.jar
The problem seems to be a missing classpath setting, i.e. a definition of the classes and libraries that are needed in addtion. In your case it seems like at least some salesforce jar has to be added to the classpath.
Either put the classpath into the manifest file or add it at the command line: java -cp <classpath here> -jar uploader.jar
When i run the command java -jar MyJar.jar i get the following errors :
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
What errors are these ? What could be the reason i am getting these errors ?
Before I packed the packages in a jar file the scene was :
Then i packed the above files along with the packages by entering :
jar -cf MyJar.jar .\Design\*.class .\InterfaceImplementation\*.class .\Interfaces\*.class .\messenger\*.class Manifest.MF RemoteMethodImpl_Stub.class
NOTE : When i unpack the jar file there is a folder named META-INF which also contains MANIFEST.MF but not the name of main class.
The content of my MANIFEST.MF : Main-Class : messenger.Messenger also tried by putting a forward slash
To add the jar is not enough to include it in the files, you have to use the m option, like
jar cmf myManifestFile myFile.jar *.class
according to jar documentation. The order of the options m and f has to match the order of the parameters for the name of the MANIFEST file and the jar file.
Take into account this warning too: An existing manifest file must end with a new line character. jar does not parse the last line of a manifest file if it does not end with a new line character.
The error is obviously due to missing main class in manifest...you can specify main class in manifest like this
Main-Class: com.Main