I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). The classes the JVM is complaining about are my classes (no third party jars required). When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about.
The jar is created using the following task:
<target name="jar" depends="">
<jar destfile="build/app.jar" >
<manifest>
<attribute name="Built-By" value="hamza"/>
<attribute name="Main-Class" value="com.hamza.driver.ui"/>
<attribute name="Class-Path" value="./"/>
</manifest>
<fileset dir="build">
<include name="**/*.class"/>
<include name="**/*.png"/>
<include name="**/*.xpi"/>
<include name="**/*.html"/>
<exclude name="**/*.jar"/>
</fileset>
</jar>
I cannot figure out what is causing the problem. If I unzip the jar and run the jar from the directory I unzipped the class to, everything works fine. So, I am assuming all the required files are inside the jar.
EDIT: com.hamza.driver.ui is a class in a package called com.hamza.driver which has main.
After the build, I get one jar "app.jar", and I run it using "java -jar app.jar", which executes fine on OS X, but not on Windows.
If I unzip app.jar in a seperate directory and run "java -jar app.jar", it excutes fine.
EDIT 2: exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hamza/gui/tr
ansfer/ClipboardTransferHandle
at com.hamza.driver.ui.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.hamza.gui.transfer.Clipboa
rdTransferHandle
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.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
ClipboardTransferHandle .class files are present in the jar.
EDIT 3: imports for the clip board class:
import java.util.logging.Logger;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.IOException;
While playing with it, I found that if I try to declare ClipboardTransferHandle as a static variable in the driver, it works, but every object that is not static is not found.
All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; everything that is created not static causes NoClassDefFound, but if I declare them static for testing, they work.
This is the problem that is occurring,
if the JAR file was loaded from "C:\java\apps\appli.jar", and your manifest file has the Class-Path: reference "lib/other.jar", the class loader will look in "C:\java\apps\lib\" for "other.jar". It won't look at the JAR file entry "lib/other.jar".
Solution:-
Right click on project, Select Export.
Select Java Folder and in it select Runnable JAR File instead of JAR file.
Select the proper options and in the Library Handling section select the 3rd option i.e. (Copy required libraries into a sub-folder next to the generated JAR).
Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar
Now what will happen is the class loader will look in the correct folder for the referenced JARS since now they are present in the same folder that contains your app JAR..There is no "java.lang.NoClassDefFoundError" exception thrown now.
This worked for me... Hope it works you too!!!
Which class is missing? Your Main-Class attribute looks a little suspect--is com.hamza.driver.ui a class or a package?
There is a chance, that the NoClassDefFoundError (I really hate this error - always drives me crazy...) isn't thrown because it doesn't find the class it tells you (-> your class) but because java can't find one of the classes that are used to instantiate that class.
I had this problem once, when a class imported another class from a different jar (in my case: an OSGi bundle) which hadn't been properly exported. Although this was a OSGi specific problem - You may have the same problems in your environment. Maybe your application depends on some classes that are present in your actual OS-X environment but not in the actual Window environment. I'm not looking at third-party libraries but at the Java implementations itself.
Good luck!
Edit
There are two more quite similar question on SO, unfortunately with no accepted solution, but maybe one of the hints over there can help in your case:
NoClassDefFound when running a jar
NoClassDefFoundError inside jar
Edit 2
Here's a similiar problem that has an accepted answer. Hope this one helps:
NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?
Have you specified the new jar in your classpath (java -cp .;new.jar MainClass.class)?
Related
So I started working with Java. I am currently on Ubuntu Platform. I installed the jdk and it was working fine during the initial stages. Then I turned to chapter 'ACCESS CONTROL' and there it started talking about the CLASSPATH and jars.
Here's what I have done.
On my Desktop I create a folder A and in that I create file Aa.java
package home.kraken.Desktop.A;
class Aa{
public doSome(){
System.out.println("AA");
}
}
On Desktop I have another folder B and file Bb.java in that
package home.kraken.Desktop.B;
import home.kraken.Desktop.A.*;
class Bb{
public static void main(String[] args){
Aa a = new Aa();
a.doSome();
}
}
Now, when I run Bb.java I get error that it can not find Aa.
Ok, I understand that CLASSPATH is something, some variable that provides the location to search for other classes. Now here are my questions
How do I know what my CLASSPATH is?
In the first statement, I have written the complete address of the folder it is in. Even then it wont find it? Does my project has to be pointed by CLASSPATH only?
Say my CLASSPATH points to Desktop [i.e. I set the value of CLASSPATH], now for every file I create, if I want to import some other class, then I will import RELATIVE_ADDRESS_TO_DESKTOP?
How do I set the value of CLASSPATH?
What exactly is jar. I understand it is the collection of Class FIles but do I explicitly make a jar file using some command or ...How does it work?
Thanks.
EDIT
So here's what I did
My classpath originally was empty so I did
export CLASSPATH="$CLASSPATH:/home/kraken/Desktop"
Now Inside folder Desktop/A, I have
package A;
public class Aa{
public void doSome(){
System.out.println("AA");
}
}
And inside folder Desktop/B, I have
package B;
import A.*;
class Bb{
public static void main(String[] args){
Aa a = new Aa();
a.doSome();
}
}
Now, I do javac Aa.java
And javac Bb.java
And when I do java Bb [ Inside folder Desktop/B To run the program, it says ]
Exception in thread "main" java.lang.NoClassDefFoundError: Bb (wrong name: B/Bb)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Bb. Program will exit.
What is happening here?
EDIT 2
When I do java B.Bb [Inside Folder B.Bb] It prints out AA.
If someone can explain what is going on?
1) How do I know what my CLASSPATH is?
It is a system environment variable. On UNIX/Linux, you type "echo $CLASSPATH" to see what it is set to.
2) In the first statement, I have written the complete address of the
folder it is in. Even then it wont find it? Does my project has to be
pointed by CLASSPATH only?
That is a package name. It tells Java where to look within the classpath. Normally, the package name doesn't match the absolute path of the file. For example, suppose I have a class located on my file system /user/local/com/stackoverflow/foo. Java needs the classpath set to /usr/local so it knows where to look for the class. Similarly, the file might be in the jar and the classpath says were to look in the jar.
3) Say my CLASSPATH points to Desktop [i.e. I set the value of CLASSPATH], now for every
file I create, if I want to import some other class, then I will import
RELATIVE_ADDRESS_TO_DESKTOP?
Yes. Your CLASSPATH points to the "root" of your package statement.
4) How do I set the value of CLASSPATH?
export CLASSPATH="$CLASSPATH:textToAddToClasspath"
5) What exactly is jar. I understand it is the collection of Class FIles but do I explicitly
make a jar file using some command or ...How does it work?
Yes, a jar is a collection of class files. You create it using "jar -cvf jarname.jar *.class" or the like
Put Aa.java and Bb.java in the same folder.
I believe that the JDK packages are set by default.
So, if you put the two files in the same folder, all you have to do is set the classpath to the folder that you have everything in.
java -classpath /home/user/myprogram org.mypackage.AA.java
or whatever your file structure is ^^
this should get your program to work
I have project called test in Eclipse which I want to export into Runnable JAR file. During the process of exporting the project, when I choose as a location of JAR file the subfolder of test folder in file system, such as C:/.../test/App.jar, it will work. However, when I want to put it for example on my desktop, it will run but in a very limited look (some GUI components missing) and it throws a lot of exceptions saying something like that:
at java.awt.EventDispatchThread.run(Unknown Source) xception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class com.sun.opengl.impl.windows.WindowsGLDrawableFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.media.opengl.GLDrawableFactory.getFactory(GLDrawableFactory.java}
What can be the problem? I assume it is not good that I have to specify my JAR file concretely into one folder in file system, it would become unuseful for any other potentional user.
Looks like the jar needs other jars to execute. In this case you would have to set the classpath before you execute the jar
This might help you with your problem.
This is a really silly question that I can't fine a difinitive answer to.
Background.
I'm using Eclipse (with one of the ANT plugins, on an XP terminal).
I have just started playing with ANT, in the [jar] directive I am setting the location of my finished JAR file and I get the following when I 'unzip' the file
META-INF/MANIFEST.MF
MyMainFile.class
which is consistent with that found on the oracle web site for the internal structure.
(here http://docs.oracle.com/javase/tutorial/deployment/jar/view.html )
But when I try to run my file I get a 'main class not found' error ?
I have seen some other posts where people have 'unzipped' the JAR file and ended up with a structure of the following...
META-INF/MANIFEST.MF
dtvcontrol/DTVControlApp.class
(from here http://www.coderanch.com/t/528312/java/java/standalone-application)
So should I get a structure where my class files are in a directory that reflects the name of the package... eg
META-INF/MANIFEST.MF
MyPackage/MyMainFile.class
and if so, why am I getting the incorrect structure using ANT, or why are there 2 different 'correct' internal structures? (how to specifify main-class and classpath for each / control what I get)
Also in case you are interested, in the MANIFEST file states (build using ANT)
[attribute name="Main-Class" value="MyPackage.MyMainFile"/]
Also the directory structure of the package under development is as follows...
/JavaDev/MyTestPackage/src (contains the source files)
//JavaDev/MyTestPackage/bin (contains the class files from eclipse, or from the ANT JAVAC task, have I named it incorrectly? should I have called it build ? )
Further to this, when I create the jar I am not naming it 'MyTestPackage.jar' but simply 'test.jar' could this be causing a problem? I assume not as if I have well understood that is what the [main-class] definition stuff is all about.
Further to all this...
'MyTestPackage' is actualy a small visual error messaging library that I use elsewhere, and I have a second file that has a main class to use for testing. As such it points to various libraries (do I need to copy all the SWT libraries to a specified directory?)
I have read somewhere that if I load libraries into my main class (which I obviously do to open the window) then trying to run the program will fail on a 'main class not found' if I use them, same is also true for adding in any 'static final' members (which I use for the loggin of errors).
'Static Final' problem...
I tried to adjust the classpath in ANT, and I get a load of other errors for the connection to Log4J and my 'wrapper' that I use (to do with it being a bad import), but the libraries exist where they should as set in the classpath).
I feel like I am almost there.... but not quite...
I'm doing this for the small 'library projects' that I am creating with the intention of using MAVAN for the main outer package that will connect them all together... for now however I just want to get this going so as it works.
I can supply the full source, or any other info as required.
Thanks in advance...
David
It's simple when you know where to look. Say your META-INF/MANIFEST.MF contains the line:
Main-Class: mypackage.MyMainFile
then the structure of the jar needs to be
META-INF/MANIFEST.MF
mypackage/MyMainFile.class
where MyMainFile has to be in the proper package:
package mypackage;
public class MyMainFile {
public static void main(String[] args) {
...
Your error message is caused by MyMainFile being in the wrong place.
Edit: it's been a while since the last time i did that with ant, but i think you need something like this: a source file structure that reflects the package struture, say
src/main/java/mypackage/MyMainFile.java
and a directory to put the compiled class file into, say
target
(I'm using maven conventions here, ant doesn't care and you can use the (rightclick)->properties->Java Build path->Sources tab in eclipse to set the source dir to src/main/java and the target to target/classes). Then in ant, have a compile target that compiles from source to target:
<target name="compile">
<mkdir dir="target/classes"/>
<javac srcdir="src/main/java" destdir="target/classes"/>
</target>
so that after ant compile you should see the class file in the target
target/classes/mypackage/MyMainFile.class
Then have a ant jar task that packages this:
<target name="jar" depends="compile">
<jar destfile="target/MyJarFile.jar" basedir="target/classes">
<manifest>
<attribute name="Main-Class" value="mypackage.MyMainFile"/>
</manifest>
</jar>
</target>
After saying ant compile jar you should have a file MyJarFile.jar inside target and
java -jar MyJarFile.jar
should run the main method.
I need to create jar file of my java app. For that first i had created a file called Manifest.MF , in that file i had stored the following code
Manifest-Version: 1.0
Main-Class: com.demo.test.JavaTest
and then i had exceuted the following command in command prompt
jar cvfm JavaAppDemo.jar MANIFEST.MF "C:\JavaSamples\MyApp"
where MyApp is my project directory, there i had created packages and used other jar files too
and then i try to run the jar using java command
java -jar JavaAppDemo.jar
and i got following Exception
Exception in thread "main"
java.lang.NoClassDefFoundError:
com/demo/test/JavaTest
Caused by:
java.lang.ClassNotFoundException:
com.demo.test.JavaSamp
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class:
com.demo.test.JavaTest. Program will
exit.
pls tell me, i want to create a jar and i have to bundle the files in my project directory to that jar files, when i extracted the jar i got my project folder, but when i run my jar i got the above exception so i got confused where the loop hole is present.
You should probably use this syntax for building your jar :
jar cvfm myapp.jar myapp.MF -C classes myclasspath
Okay well a few things:
Here is how you should run your application:
java -classpath myapplication.jar:.. -Dlog4j.info -Dlog4j.configuration=file:../../conf/log4j.xml com.mywebsite.Benchmarks
Take that log4j stuff out if you don't need it. I am sure you are just missing the "com.mywebsite.Benchmarks" line which tells the jar which compiled class to start with. If you have Benchmarks, FullTest, MainApp all compiled in your jar and the main method is in Benchmarks, you need to tell the jar that.
As far as building your jar...
It is the easiest to use some kind of ant build script.
<target name="build">
<delete file="MyApplication.jar" />
<delete dir="_classes" />
<mkdir dir="_classes" />
<javac srcdir="src" destdir="_classes" debug="true">
<classpath path="../../3rdParty/log4j/log4j-1.2.16.jar" />
</javac>
<jar jarfile="MyApplication.jar" basedir="_classes"/>
</target>
That should get you started. But you need to make sure of a few things when your run it:
You have all the dependencies on your class path.
You tell the jar which class to start with.
Some things I can think of:
The error message says that it can't find the file com/demo/test/JavaTest.class. You could try looking at your jar file with a zip file program (a .jar file is compressed just like a .zip file) to see if that file is really not in there. I suspect the error code is actually correct in that it is not actually there, but at least you can see it (or not see it) with your own eyes. And remember that it has to begin with a "com" folder.
Perhaps you are confusing .class files and .java files. Are you sure your .java files are actually compiled into .class files? This might be unlikely, but it may as well be said.
Maybe you need to run your jar command with "C:\JavaSamples\MyApp\*" at the end to pick out all the files in the right directory. Maybe you were telling the jar command to include the folder "MyApp". You don't actually want that folder; you want all the files inside it.
I'd like to make "double-click" cli application but still don't get how.
I know I should propably somehow edit manifest but that is all. I googled ofc. but no success.
Thanks for any tips.
Here is the output from build, run, and manifest:
compile:
Created dir: /home/nick/NetBeansProjects/SemestralWork/dist
Building jar: /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "/home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar"
jar:
BUILD SUCCESSFUL (total time: 1 second)
java -jar /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: semestralwork.Main. Program will exit.
MY MANIFEST created with build:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.0-b08 (Sun Microsystems Inc.)
Main-Class: semestralwork.Main
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
These two lines tell you all you need to know:
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
And a further clue is dropped by the manifest output:
Main-Class: semestralwork.Main
This means that the JAR file is looking for a package named semestralwork and a class named Main inside it. It fails at this point because it cannot find either the semestralwork package or the Main class.
As you pointed out in your question, the problem is indeed in the manifest file. You can edit this directly in your JAR file if you like, but a better idea would be to do this from Netbeans:
Click on `File --> Project Properties (semestralwork)'
In the dialog that opens, on the tree on the left select Run
Then, on the right, under the field labeled Main class:, enter the fully qualified class name of the class that you want executed when run from the command line.
In your case, as I see from your comment on #Aaron's answer, if your main class is in a file called encryption.java, and it is in the default package (no package), just enter encryption.
Once this is done, do a clean and build, then try running it from the command line again.
HTH
Since I encountered the same problem, I can clarify the solution a little bit.
You must create main Java class outside your method (for example-default_package folder), and then invoke your method(folder), e.g import your_folder.connected_class; in that main class.
Hopefully I could help someone with the same problem.
It's easier to make a .exe from a .jar without netbeans.
Here are my suggestions:
1. Use a special application for this(ex: JSmooth, JEXECreator etc)
2. Make a C++ program that starts a JVM (see this tutorial)
The default class search path may be the issue. You should try changing directory to the location the jar is and launching with java -jar Semestral.jar. Also you may have misnamed the main class. Please also include your package structure.
Open the JAR file with a ZIP tool (or try less ... if you're on Linux or jar tvf ...). Make sure there is a directory semestralwork in there which contains a file Main.class.
It's Easy. Download a copy of netbeans. Make a new project in netbeans. Goto your main class in the Projects Explorer. This should be folder "YourProject" As the class YOURPROJECT.JAVA . It is this yourproject.java file that you want to start with. Just write your code into the public static void main area. You can run the program with the green play button in the top toolbar.