Why adding class path caused main class not found? - java

I get a NoClassDefFoundError when trying to run a Java class without providing the proper class path. However when added the needed class path, java complaints it cannot find the main method. If you have any idea of what's happening here, please point me in the right direction. Thank you
$ java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix1D
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
... 6 more
$ java -cp resources/colt.jar MyClass
Error: Could not find or load main class MyClass

Assuming MyClass isn't in a package, you need something like (on Windows)
java -cp resources/colt.jar;. MyClass
or (otherwise)
java -cp resources/colt.jar:. MyClass
To also include the current directory. Alternatively, you can set the CLASSPATH environment variable.
On Windows,
set "CLASSPATH=resources/colt.jar;."
otherwise something like (depending on your shell)
export CLASSPATH="resources/colt.jar:."
then
java MyClass

try to include the current directory in the classpath as well. usually we add new jars to classpath liek this:
java -cp %CLASSPATH%;resource/colt.jar MyClass
or on Linux as:
java -cp $CLASSPATH:resource/colt.jar MyClass
Additionally u can also add .
i.e the currrent directory to the classpath.

Related

TestRig / grun drops java.lang.NoClassDefFoundError when using the output directory -o option

Following this post, I am using the following steps to compile the parser/lexer from this repository:
export CLASSPATH=".:/usr/local/Cellar/antlr/&ltversion&gt/antlr-&ltversion&gt-complete.jar:$CLASSPATH"
antlr &ltgrammarName&gt.g4 -o &ltsomeFolder&gt/
javac &ltsomeFolder&gt/&ltgrammarName&gt*.java
but when I use the instructions here:
grun <someFolder>/<grammarName> tokens -tokens < <inputFile>
I get this error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: IllegalName: &ltsomeFolder&gt/&ltgrammarName&gtLexer
at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:889)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.antlr.v4.gui.TestRig.process(TestRig.java:129)
at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
I would appreciate if you could help me know what is the problem and how I can resolve it.
I don’t see where you’ve specified a package name, so now your Java classes are located in <someFolder>. Be sure to compile them in that folder.
Then you’ll need to add that folder to your classpath (probably instead of “.”)
Try adding <someFolder> into the CLASSPATH you’re exporting. Then leave it off of your grun command line.
Java will only load classes from the Classpath (it’s a security thing). When TestRig runs, it attempts to load your class by building the Java class name it would have produced for you Parser (and Java will have to find that class somewhere in the classpath).
Your could modify the grun alias to allow for you to specify a directory to search for your classes, and use the -cp option on the Java command, but that’s probably more trouble than just adding it to you classpath that you’re using for this testing.

How to specify the MainClass for java

I am trying to make a .bat the script just for running a java file where I try to use the JNI, but I have an interesting problem.
The directory where the script should be is called NativeMethodTest and contains a folder called out
Now when I am in the out folder I can call my main class just with
java HelloWorld
and it just works fine, but when i am in the parent folder it seems to start runnig with the command
java -cp "./out" HelloWorld
but i get this Error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path
at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
at java.base/java.lang.System.loadLibrary(Unknown Source)
at HelloWorld.<clinit>(HelloWorld.java:8)
but the HelloWorld.java contains this line
System.setProperty("java.library.path", "D:\\projects\\NativeMethodTest\\out\\.");
which sets my library path absolute, so it should work and it does when I am in the same folder, but not in the parent, please help
thanks
here the folder structure:
./NativeMethodTest/run.bat
./NativeMethodTest/out/hello.dll
./NativeMethodTest/out/HelloWorld.class
ok, this is weird since the getProperty("java.library.path")method gives the path, so i made a false assumption, but thanks to Elliott i fixed my script to java -cp "./out" -Djava.library.path=".\out\." HelloWorld
which now works fine from the parent too, thank you have a nice day

noclassdeffound error in java, linux

running a java class by shell script:
java -cp $CLASSPATH CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
while running the script in unix, getting error,
Exception in thread "main" java.lang.NoClassDefFoundError: CG_GpsRequest
Caused by: java.lang.ClassNotFoundException:
CG_GpsRequest is the class file name.
Okay.. If I understand you correctly..
I think its problem with the Package name specification..
Your calss CG_GpsRequest you must have specified in Packaging way for example
com.xxx.yyy.CG_GpsRequest
So I think JVM is searching your class com.xxx.yyy.CG_GpsRequest in this patter so Please run your script with the following modifications.
java -cp $CLASSPATH com.xxx.yyy.CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
This is just assumption that you have created your class in Pacakgin hirarechy since you havent specified more information.
Add the folder/path in which your java class exists to your CLASSPATH

Java NoClassDefFoundError even though the class exists

I'm trying to figure out how class importing works in Java. I have the following file structure:
testProject
pkgA
A.java
A.class
dir
pkgB
B.java
B.class
The contents of A.java are:
package pkgA;
public class A {
public static String funcA() {
return "funcA in class A in pkgA";
}
}
The contents of B.java are:
package pkgB;
import pkgA.A;
public class B {
public static void main(String[] args) {
System.out.println((new A()).funcA());
}
}
In testProject I run:
javac pkgA/A.java
The above command doesn't print anything.
In testProject/dir I run:
javac pkgB/B.java -classpath ..
The above command doesn't print anything.
In testProject/dir I run:
java pkgB/B -classpath ..
The above command prints the following:
Exception in thread "main" java.lang.NoClassDefFoundError: pkgA/A
at pkgB.B.main(B.java:7)
Caused by: java.lang.ClassNotFoundException: pkgA.A
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
What am I doing wrong?
Giving the classpath as an absolute path doesn't help.
$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
OS is Ubuntu 12.04
Thanks!
Note that your classpath spec must preceed classes or .java files e.g.
java -classpath .. pkgB/B
(the same applies to the javac invocation).
I would compile everything at the same time e.g.
javac -classpath {whatever} {complete list of .java files}
for consistency's sake.
Specify the compiler output directory to be separate to your source (e.g. a directory called classes). That makes life simpler in terms of managing your code and the compiled artifacts.
Going forward, you should investigate a build tool such as Maven or Ant (or Gradle or Sbt etc.). That will make life much more manageable as you add source files, config or dependencies.
Check your classpath and make sure the current directory(directory that holds pkgA and pkgB) is in the classpath.
It's not always about the class you first see, it's also about what's in them.
1) You have a class called "com.my.Clazs".
2) The class imports "com.my.other.Clazzzz";
3) You have a static method inside Clazs
public static void doFoo() {
/* The call to Clazs.doFoo() will result in NCDF Error if you
* skip Clazzzz lookup in classpath.
*
*/
Clazzzz anObject = new Clazzzz();
}
if you are trying do something from Clazs statically, it will give you NoClassDefFoundError because it cannot load all the dependencies fully to qualify this as a valid class. In other words, all the URLs cannot be resolved fully.
This kind of problem is not unusual even when you have dependency manager e.g. Maven. You might forget that you have a compile-time dependency on certain jars/classes (e.g. Java Tools.jar) which you also need to load when you start the application.

exception in thread 'main' java.lang.NoClassDefFoundError

When I try to execute the following program from DOS I get the results below..
The following program is in C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src\java1
package java1;
public class MyProgram{
public static void main(String[] args){
System.out.println("Rome wasn’t burned in a day!");
}
}
javac MyProgram.java
works fine
But java MyProgram gives the following:
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
: java1/MyProgram)
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)
................
Why this is happening??
My environment variables:
CLASSPATH: .;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_05\bin
PATH: C:\Program Files\Java\jdk1.7.0_05\bin
JAVA_HOME: C:\Program Files\Java\jdk1.7.0_05
I have seen similar problems here but i cannot find the solution to my problem.
Thank you in advance!
This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Refer this Link
2 points you should keep in mind when using java tool:
Add the class to the classpath.
Use the fully qualified name of the class to be run.
Hence:
java -cp C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin java1.MyProgram
assuming the following file exists after compilation:
C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin\java1\MyProgram.class
For more info, see:
Mastering the Java CLASSPATH.
"Setting the class path" Documentation.
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.
Try in this way
run command prompt as Administrator, and
cd C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
then
javac java1/MyProgram.java
Then
java java1.MyProgram
This will work.
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
This exception is thrown when the JVM cannot finf your class at run time
From C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
execute " "java java1.MyProgram" –

Categories

Resources