Issue in creating and running a .jar file - java

I have compiled a java project into a jar but I am having some issues when trying to run it
java -jar XMltoCSV.jar
Error: Could not find or load main class XMLtoCSV.class
I have created a manifest file that contains:
Main-Class: xmltocsv.XMLtoCSV
(there is a newline character in the manifest text file)
The XMLtoCSV.class does contain the main() method. I have been running this code to create the jar file:
jar cvf XMLtoCSV.jar Manifest.txt xmltocsv/*.class
And I know its working because I get:
jar cvfm XMLtoCSV.jar manifest.txt XMLtoCSV.class
added manifest
adding: XMLtoCSV.class(in = 4885) (out= 2492)(deflated 48%)
Why does it give me this error? Thanks in advance for the help
It seems I am also running into another error, what does it mean?:
Exception in thread "main" java.lang.UnsupportedClassVersionError: xmltocsv/XMLt
oCSV : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: xmltocsv.XMLtoCSV. Program will exit.

Main-Class: XMLtoCSV.class
it's not right.
You have to specify something like:
Main-Class: com.mydomain.XMLtoCSV
namely the fully qualified name of the class.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Drop the .class in your manifest.
Update:
What is the package of your main class? If your Java-File contains a statement such as
package com.google.common.base;
at the top. You need that package in your manifest. In your case that would be:
Main-Class: com.google.common.base.XMLtoCSV
If your class does not have such a package statemnent the error is something else.
(Note that com.google.common.baseis just an example of course.)

For a jar be executable, you have to be concerned with two thing on the manifest file:
1- The 'Main-Class' entry must be pointing to the class that'll be launching the application, i.e. the class where you declared the main method to start your app.
2- After all entries on the manifest file, you have to leave three lines in blank (I never found a documentation explaining why that is required but always works for me).
The simplest MANIFEST file would look like the following:
Manifest-Version: 1.0
Main-Class: com.foo.FOO
Just remember to leave three lines in blank after the last entry.
Hope I have helped.

Related

Problems exporting Jar using Weka

i've got a big problem exporting my project as a jar file (inside my project i use weka). I'm using Eclipse.
I've added in Java Build Path weka.jar and i've attached to it weka-src.jar, testing my classifier under eclipse everything works, so i've decided to create a jar (because i need that another program use it).
First of all i've created the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ weka.jar weka-src.jar
Class-Path: .
Rsrc-Main-Class: start.Home
Main-Class: start.Home
The main class of the program is Home (inside the package start), while weka.jar and weka-src.jar are inside the /src/ directory of the eclipse program.
When i try to execute the jar created i receive this output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/Classifier
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: weka.classifiers.Classifier
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)
... 7 more
I think that the problem is the CLASSPATH so i've tried to solve it in this way:
I've created the Environment Variable CLASSPATH with this values:
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka.jar
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka-src.jar
but nothing changed, can someone help me please?
You are right: the classpath is the problem. Did you remove the items from the manifest classpath when you added them to the environment CLASSPATH? (Side note: place the source jar in the -sourcepath, not the classpath.)

Additional jar files for java.exe -jar

I run the following command:
C:\Projects\java -cp ./dependency.jar -jar ./dist/main.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyException
Caused by: java.lang.ClassNotFoundException: MyException
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: Main. Program will exit.
MyException is inside dependency.jar. I also tried to replace -cp with -classpath. What do I do wrong?
The -cp options is ignored if you use -jar. If you need extra jar-files on the class-path, you should specify this in the Manifest of main.jar like this:
Class-Path: dependency.jar
(Then you can run your program using java -jar main.jar.)
Related questions:
When creating a jar file how do I retain the existing classpaths to other jar files?
Cannot find class even when jar file is in working directory
Jetty Classpath issues
problem related to MANIFEST.MF in jar
Have you tried specifying the class path in the windows environment variable CLASSPATH ?
specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and (path to all your classes) and also include current directory by including (dot or period sign).

Problems running java classfile from the folder

I have three source files in a folder. I simply want to compile them using commandline and then execute them. However, I'm having the following issue. I am on a windows box:
Code compiles fine:
C:\mycode\src\code>javac Source1.java Source2.java Source3.java
Does not run from the folder where class files are:
C:\mycode\src\deckofcards>java Source1
Exception in thread "main" java.lang.NoClassDefFoundError: Source1 (wrong name: code/Source1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: Source1. Program will exit.
However, it does run when moving to parent folder:
C:\mycode\src\code>cd ..
C:\mycode\src>java code/Source1
Hello1
Hello2
Hello3...
......
....
I want to distribute this source to someone and I'm not sure whether they will be on windows or unix box.
Eventually I want to create an executable jar file but that also was giving me an error like this.
Problem here is not with running Java files, it runs fine. So you wanted to understand the following:
In order to run a Java program you need to do the following steps:
Create Java source files (*.java files)
Compile using javac to binary class files (*.class files)
Run the class files using java executable
We use package structure to keep organize the java files. In your case it is a simple application (and you use the package named "code"), but in a large production application we need to organize our code into many packages.
While running the class file, we need to give the class file path (which includes the package path). So in your case you need to execute java code/Source1 to run your class.
You can learn more from this link: http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
For creating a JAR file you can refer the link: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
For making a JAR file executable ensure you add manifest file with main-class attribute.

How to run java application without using IDE?

I have a java application where main class is dependent with some other classes. I can run the application using an IDE well. But when I want to run the application from command line by locating to the main class's class file using java mainClass, it gives me the following error [the main class name is mainClass]:
Exception in thread "main" java.lang.NoClassDefFoundError: mainClass(wrong name: mainfolder/mainClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can anybody suggest me how can I run my application without using any IDE?
Uou need to indicate the fully qualified name. For example if MainClass is under package com.example, you should execute java com.example.MainClass
You can check more here.
From documentation:
By default, the first non-option argument is the name of the class to
be invoked. A fully-qualified class name should be used. If the -jar
option is specified, the first non-option argume nt is the name of a
JAR archive containing class and resource f iles for the application,
with the startup class indicated by the Main-Class manifest header.
The Java runtime searches for the startup class, and other classes
used, in three sets of locations: the bootstrap class path, the
installed extensions, and the user class path.
Try mainfolder.mainclass instead of mainfolder/Mainclass. Also, the mainfolder is the package.
Make sure you put all needed jars in the classpath with -cp.
With or without IDEs, java application can always start by
java -cp classpath EntryClass
If you run the application with IDEs, the IDE you using prepares the classpath for you. If you run without the IDEs, you should prepare the whole classpath yourself.
The EntryClass is also found through the classpath.
Please refer to java - the Java application launcher for details.
Let say you are runngin your java program from /home/flyleaf/app/
This would be the command you need:
javac -classpath /home/flyleaf/app/jar1.jar:/home/flyleaf/app/jar1.jar:/home/flyleaf/app/ com/app/your/mail/class/Main.java
You need to add to the classpath the jar files you need and the path where you got your program
Ok! Got the output after executing the java command over the main class's class file with package.classname from outside the folder that contains the class files!
Besides the jar file is also working after locating the jar file and by executing this command java -jar jarname.jar!
Thanks everybody!

Error "main class not found"

All,
My classpath has been set to the following folder:
CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project
All my java files and class files are under project folder listed in CLASSPATH.
Yet I am getting the following error while running a DriverClass in project folder:
C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: DriverClass. Program will exit.
Can anyone please help me understand the reason here?
You're inside the package. You should be sitting in the package root. Go one folder up.
cd ..
Then reexecute it using java project.DriverClass.
That said, you should prefer not to use the CLASSPATH environment variable. Yours is currently also invalid. There are spaces in unquoted pathnames. Also, the JDK/bin folder isn't supposed to go in the classpath. Just use the -cp argument like java -cp . project.DriverClass (while sitting in the package root folder).
Try:
C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass
assuming your package is project
After compiling if we get a main class not found error, It is simple try this
set classpath=%classpath%;.;
type this in cmd and run it..problem will be resolved

Categories