I could not understand this error, the scenario is:
This is the directory structure:
classes/
root/
pkgmain/Cmain.class
pkg1/C1.class
pkg2/C2.class
The problem is when I try to run Cmain file (main() is in this file) from the following path, I get the error:
C:\Ravi\MakingJarFile\classes\root\pkgmain>java Cmain
Exception in thread "main" java.lang.NoClassDefFoundError: Cmain (wrong name: root/pkgmain/Cmain)
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: Cmain. Program will exit.**
What could be the solution for this problem, Thanks.
You need to provide the fully qualified class name, and do it from the root directory (or put that directory in the classpath):
C:\Ravi\MakingJarFile\classes> java root.pkgmain.Cmain
or
C:\Anywhere> java -cp C:\Ravi\MakingJarFile\classes root.pkgmain.Cmain
You're telling the java command the full name of a class, and that's what it's looking for. When it finds a relevant class file it will check that the class within it really does have that fully-qualified name
You have to specify the full path to the class - otherwise java can't find the Class.
java root.pkgmain.Cmain
Related
I am recently making a PluginClassLoader to load java plugins to my programs dynamically. So that the plugins can interacts with my program.
However, things won't go nicely though. So I looked up with bukkit's implementation of loading plugins. [Bukkit's source code] I followed with it, Java still throws me a NoClassDefFoundError.
Here's my implementation:
All the plugins that to be added are extended to com.mob41.sakura.plugin.Plugin. This abstract class contains onCallPlugin(), onEndPlugin(), etc. functions for the main program to interact to them. [Abstract class source code]
All the plugins will be placed in a /plugins folder on the working directory (System.getProperty("user.dir") + "\\plugins"). My program will find the folder (/plugins) on the working directory. If it doesn't exist, it creates a new folder. If exists, it will find all the jar files. And create a PluginClassLoader [Source Code] to add the plugins.
I expected the plugins will be added to the my program and the classloader will create their instances. Through this testing code:
public static void main(String[] args) {
System.out.println("Loading...");
try {
PluginManager.getPluginManager().loadAllPlugins();
} catch (Exception e){
e.printStackTrace();
return;
}
System.out.println("Loaded.");
JSONObject json = new JSONObject();
json.put("ext", false);
System.out.println((JSONObject) PluginManager.getPluginManager().runPluginLifeCycle("Sakura-Plugin-HKOWeather", json));
}
If the plugin invalid/causes error when loading it, this exception will be thrown.
I don't know why my program cannot find the plugin that extended itself...
com.mob41.sakura.plugin.exception.InvalidPluginException: java.lang.NoClassDefFoundError: com/mob41/sakura/plugin/Plugin
at com.mob41.sakura.plugin.PluginManager.loadPlugin(PluginManager.java:180)
at com.mob41.sakura.plugin.PluginManager.loadAllPlugins(PluginManager.java:145)
at com.mob41.sakura.Main.main(Main.java:12)
Caused by: java.lang.NoClassDefFoundError: com/mob41/sakura/plugin/Plugin
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.mob41.sakura.plugin.PluginClassLoader.<init>(PluginClassLoader.java:25)
at com.mob41.sakura.plugin.PluginManager.loadPlugin(PluginManager.java:176)
... 2 more
Caused by: java.lang.ClassNotFoundException: com.mob41.sakura.plugin.Plugin
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 17 more
Through hours of debugging, I finally found the answer.
I was specifying a wrong ClassLoader to my implementation of PluginClassLoader.
Dev env: ItelliJ 14 -> compiles and runs fine.
When executing my custom created .jar from command line this appears
C:\Java\Projects\OpenGLES\out\artifacts\Test1>java -jar Test1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/jogamp/opengl/G
ventListener
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)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLEventListener
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)
... 13 more
I'm trying to create .jar artifact, that I could just double click start.
I think that the problem means that this class cannot find some resources. The JOGL/JogAmp official sites gives explanation on deployment of .jars with JOGL, but I think that I missed something.
The Test1.jar has following structure:
Test1.jar/
--com/
----company/
------Main.class
--jar/
----gluegen-rt.jar
----gluegen-rt-natives-windows-amd64.jar
----gluegen-rt-natives-windows-i586.jar
----jogl-all.jar
----jogl-all-natives-windows-amd64.jar
----jogl-all-natives-windows-i586.jar
--lib/
----windows-amd64/
-------gluegen-rt.dll
-------joal.dll
-------jocl.dll
-------jogl_cg.dll
-------jogl_deskop.dll
-------jogl_mobile.dll
-------nativewindow_awt.dll
-------nativewindow_win32.dll
-------newt.dll
-------soft_oal.dll
--META-INF/
----MANIFEST.MF
--com
--jar
--lib
--META-INF
manifest.mf contains -
Manifest-Version: 1.0
Main-Class: com.company.Main
By the stack trace it seems that the JVM cannot find the class com.jogamp.opengl.GLEventListener that means it can't resolve the path to the included jar files. The path of the jar files is not correct. Follow the link to see how to package jars correctly.
https://docs.oracle.com/javase/tutorial/deployment/jar/
i just downloaded a java program TableDrivenVaccumAgent.java while i was searching some robot class examples, i have included the requisite jars in the path C:\Program Files\Java\jdk1.7.0_25\jre\lib\ext but i am getting error after it compiles successfully.
what should i need to do more to resolve the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: TableDrivenVacuumAgent (wrong name: aima/core/environment/vacuum/TableDrivenVacuumAgent)
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)
now error is:
Exception in thread "main" java.lang.NoClassDefFoundError: TableDrivenVaccumAgent (wrong name: aima/core/environment/vacuum/TableDrivenVaccumAgent)
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)
NoClassDefFoundError in Java comes when Java Virtual Machine is not
able to find a particular class at runtime which was available during
compile time. For example if we have a method call from a class or
accessing any static member of a Class and that class is not available
during run-time then JVM will throw NoClassDefFoundError.
Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why it’s not available in Classpath if we are expecting it to be. There could be multiple reasons like:
Class is not available in Java Classpath.
You might be running your program using jar command and class was
not defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
I am trying to learn how to compile and run using only command lines in Windows. Here is the tree of the directories starting from the root:
D:
ActivityOne
- classes
- com
-wat
-sampleapp
-students
StudentE.class
StudentMasterList.class (Main)
- src
-com
-wat
-sampleapp
-students
StudentE.java
StudentMasterList.java (Main)
The thing is that I am now confused as to how to run the program.
I tried two things, where both returned different errors.
1st try:
java -classpath classes StudentMasterList
returned:
Error: Could not find or load main class StudentMasterList
2nd try:
java -classpath classes/com/wat/sampleapp StudentMasterList
returned:
Exception in thread "main" java.lang.NoClassDefFoundError: StudentMasterList (wrongname: com/wat/sam
pleapp/StudentMasterList)
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)
I got confused after the next tries, any help on how i should run the main class?
Update: I should run all my commands at the ActivityOne level.
The correct way is
java -classpath D:\ActivityOne\classes com.wat.sampleapp.StudentMasterList
In other words, you add the top-level directory to the classpath, and then use the fully-qualified name of your Java class.
i'm kind of new to java. I use eclipse and i imported some jar files with some classes by going to properties -> Add External Jars;
The problem is that when i try to use a class from the jar i get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: nextapp/echo2/app/event/ActionListener
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$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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at Program.main(Program.java:12)
Caused by: java.lang.ClassNotFoundException: nextapp.echo2.app.event.ActionListener
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)
... 13 more
The class is supposed to create a window,
i guess that the jar has to import somehow other stuff or something like that,
How can i fix this?
PS. Sorry for the bad code , i don't know tags here.
thansk,
Raxvan
Most jars you download will include 0-5 other jars that are needed. They all need included in order to use the jar. Very quickly you end up with 20 jars in a simple 1000 line Java project, but thems are the breaks.
Look inside your downloaded jar zip. Should be a lib directory or such with all the libs you need.
Another way to get around this would be to use a tool like Ivy or Maven to manage downloading dependencies. Depending on what libraries you are using, you may have many transitive dependencies, and doing that all by hand will end up taking a lot of time.