I'm writing a program in Eclipse, and running it from the command line. In an earlier version of the program, it took no arguements, and I could run it fine as > java foo. I've since added a couple of arguments, and need to run it as > java foo file1.txt file2.txt. When I run this, I get a java.lang.NoClassDefFoundError: error. Even when I include the classpath, i.e. > java foo file1.txt file2.txt -cp . it still doesn't work.
Could someone point me in the right direction?
EDIT
Here is the complete stack trace
Exception in thread "main" java.lang.NoClassDefFoundError: edu/cuny/pausePred/TemplateToCharTestVector (wrong name: edu/cuny/pausepred/TemplateToCharTestVector)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Write the full directory path then compile all classes
Write the full directory and run the class that contains the main() method
A common mistake for beginners in using Java is misunderstanding class names and the classpath.
A class name is a fully qualified thing which includes the package; the compiler lets you refer to a class using its base name, which is a convenience to keep programming sane. The actual name of your class is <package>.foo.
The classpath must include the root of any packages which you are using. So if your package for foo is edu.cuny.pausePred then the class name for foo is edu.cuny.pausePred.foo and the classpath must include the directory which contains edu, not the directory which contains foo.
You command line should be something like:
jave -cp the-directory-root-for-java-sources foo file1.txt file2.txt
noting that this assumes that the two data files are in the current directory.
As an aside, note that a class base name should be lead uppercase, so Foo, not foo.
Exception in thread "main" java.lang.NoClassDefFoundError:
edu/cuny/pausePred/TemplateToCharTestVector
(wrong name: edu/cuny/pausepred/TemplateToCharTestVector)
Paths in java are case-sensitive. pausepred is not the same as pausePred
Related
I've been browsering the Internet for a couple of hours and I am unable to find an answer to my question. The library I'm trying to add is JGraphT
I'm new to Java and I wanted to add a free graph library. I downloaded all .jar files and then the issues startet. What step by step should I do?
I found information about compiling with -cp or -classpath or addng .jar to CLASSPATH (I'm using Linux and I write my programms in gedit (obligatory for my studies) and compile it with terminal). But I wonder what should I do step by step?
What do I have so far:
I have downloaded multiple .jar and they all sit in one folder with
the xxx.java file I would like to compile
do I need to change CLASSPATH? How to do it? How should I compile and
run my program after changing CLASSPATH? The ordinary way(javac
xxx.java; java xxx) or should I change sth?
or maybe I don't need to change CLASSPATH just add -classpath while
compiling? If so, what should the compile anr run commend look
like?
Also I have already tried using -cp... I'm enclosing my lines in terminal. It compiled correctly, but when I tried to run it, I received strange errors. I'm sure the code is correct since it was given in the library as a way to test wether or not is it installed correctly.
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ javac -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT.java
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ java -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT
Exception in thread "main" java.lang.NoClassDefFoundError: HelloJGraphT (wrong name: org/jgrapht/demo/HelloJGraphT)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$
Let's say your class belongs into the package net.example.graph. This means the real name of the class is net.example.graph.HelloJGraphT.
This also means you have a directory structure like this:
project_dir/net/example/graph
Go to the project_dir folder, then try this:
javac -cp <path to jgrapht JAR> net/example/graph/HelloJGraphT.java
java -cp <path to jgrapht JAR> net.example.graph.HelloJGraphT
I have a source file named Plane.java. I am trying to compile and run the program in terminal, but i can't!
I am compiling it with:
javac Plane.java
So, I am getting with ls my classes
CargoBay.class
CleaningEmployee.class
Employee.class
EquipmentComponent.class
MaintenanceEmployee.class
PassengerComponent.class
Plane.class
Plane.java
PlaneComponent.class
PrivateComponent.class
SecurityEmployee.class
Now, I have no idea how to run it! I am trying with java Plane, but I keep getting errors. Any ideas ?
(errors: after java Plane)
Exception in thread "main" java.lang.NoClassDefFoundError: Plane (wrong name: plane/Plane)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The program runs just fine in netbeans
Compile your class by
javac -d . Plane.java
This will create a plane directory at current location and put your classes into that directory then run your program using
java plane.Plane
Your classes are probably in a package named plane. The compiler creates a directory with the package name. What you have to do is go up one level from there
and call:
java plane.Plane
Other options would be to set the CLASSPATH environment variable or use the -cp option of the java command.
I compiled successfully my program but got a undefined class error when trying to run it. The weird thing is I don't use the cern/colt/matrix/DoubleMatrix1D class in myprogram (I verified this with "grep"). Can someone please point me in the right direction? Thanks
$ javac -cp $(find ../resources/ -name "*.jar"|tr "\n" ":") myprogram.java
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ java myprogram
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)
at java.lang.Class.getMethod(Class.java:1668)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: cern.colt.matrix.DoubleMatrix1D
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)
... 6 more
NOTE:
DoubleMatrix1D is an abstract class.
I used the subclass 'SparseDoubleMatrix1D' of DoubleMatrix1D in myprogram.
You may not use that class directly / explicitly in the source code of your program. But you definitely do use it ... indirectly.
For example:
It might be the type of an internal field of some class that you are directly using.
It might be a superclass of a some class that you are using directly.
And a few other things ...
Anyhow, the bottom line is that your code won't run unless you put the JAR (or whatever) containing the missing class onto the runtime class path.
Since the missing class is a superclass, the JRE needs it because it needs the code of the constructor for the superclass for when you create an instance of the subclass.
I tried to execute CreateTextFileTest.class file with classpath in the terminal as the following:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile" CreateTextFileTest
My class files in this directory : /home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile .
I have 3 classes in the directory: AccountRecord.class , CreateTextFile.class, and CreateTextFileTest.class
But, when I executed the CreateTextFileTest from the terminal with the code above, an error occured like that:
Exception in thread "main" java.lang.NoClassDefFoundError: CreateTextFileTest (**wrong name**: unit17/creatingtextfile/CreateTextFileTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As far as I understand because CreateTextFileTest.java uses two different classes, executing fails. How to handle this situation? How to run my java project? How to use classpath in this situation? Am I using wrongly?
The error message states that the package name declared in your compiled class doesn't match what the JVM was expecting given your classpath setup. The classpath should point to the directory from which your packages start, not the directory actually containing your .class files. Given your error message I believe that this should work:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17" unit17.creatingtextfile.CreateTextFileTest
Given the duplicate unit17 in your path I get the impression there's something else mixed up here. You'll get a better answer on Stack Overflow...
I am unable to run .class files from CMD, and I can't seem to find a way to get around the error messages. My environment variable is set to C:\program files (x86)\java\jdk1.7.0_45\bin, both java and javac are version 1.7.0_45 and I am running the code in the local directory:
C:\Java\hfjavafinalsamples\chap01> javac PhraseOMatic.java
C:\Java\hfjavafinalsamples\chap01> java -classpath . PhraseOMatic
Exception in thread "main" java.lang.NoClassDefFoundError: PhraseOMatic (wrong n
ame: chap01/PhraseOMatic)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
C:\Java\hfjavafinalsamples\chap01>
The fully qualified name of the class is chap01.PhraseOMatic, because its simple name is PhraseOMatic, and its package is chap01.
The java command expects a fully qualified name. Based on its classpath and on this fully qualified name, it will look for the .class file. So, if your classpath is '.' and the fully qualified name is chap01.PhraseOMatic, it will look for ./chap01/PhraseOMatic. So that won't work, since you're already inside the chap01 folder package.
The classpath should thus be .., or (better) you should be in the hfjavafinalsamples folder to run your application:
C:\Java\hfjavafinalsamples> java -classpath . chap01.PhraseOMatic
Also, you should not put the source files (.java) and the compiled files (.class) in the same folder tree. Create an src folder containing the source tree, and a classes file containing the compiled classes tree. and use the -d classes option of javac to compile the classes to the classes folder.