can not run java program on linux terminal [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Exception in thread “main” java.lang.NoClassDefFoundError: DiServer <wrong name: ds/DiServer>
java.lang.NoClassDefFoundError when i run java file from terminal
I have a program with multiple classes and compile them on the terminal with javac *.java and then try to run the main file by simple doing java filename but it spits out the following error. I don't know what it wants. Help will be appreciated.
Exception in thread "main" java.lang.NoClassDefFoundError: lab6 (wrong name: lab6/lab6)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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)

The error message means that you tried to execute your class using java lab6, but it's actually called lab6.lab6 (i.e. it's in a package called lab6 and its simple name is lab6).
To execute it, you point your classpath to the directory containing the directory lab6 and execute java lab6.lab6.
The easiest way is to go into that directory (whatever it is called) and execute java -cp . lab6.lab6.

Related

Not Able to Compile Java Code via Command-line

I wrote some Java code that takes several arguments using Eclipse. I was able to compile and execute the code just fine by entering the arguments in the "Run configurations."
However, I need to be able to run my code via command-line. This is what I tried:
javac -g ./MyCode.java
java MyCode ./fileOne.txt ./fileTwo.txt ./fileThree.txt
And the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/MyCode
Caused by: java.lang.ClassNotFoundException: mypackage.MyCode
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I don't understand what is going on. My code does not have any bugs when I run through Eclipse. Help?
You are not setting up the classpath properly.
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

trying to compile a java program in terminal [duplicate]

This question already has answers here:
NoClassDefFoundError: wrong name
(8 answers)
Closed 9 years ago.
I'm just trying to compile a simple hello world file via the terminal.
Here's the code for Hello.java:
package Hello;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
this is saved in another folder, so in terminal I typed:
cd code/repositories/java to navigate to the correct directory (where I saved Hello.java)
I next typed javac Hello.java and hit return. It compiled without any errors.
I then tried to open the file with java Hello and it threw the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: Hello/Hello)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247
How/why does this happen and how do I go about fixing it?
The package is called Hello which makes the full qualified name Hello.Hello which means you need to have your Hello.java file in code/repositories/java/Hello
then cd to the code/repositories/java folder and call javac javac Hello/Hello.java then you can run it java Hello.Hello
On a different note, Java standard naming conventions recommend package names use lowercase letters so consider changing the package to "hello"
Remove line package Hello;. Compile and run.

Java NoClassDefFoundError in Command Line

I'm getting the NoClassDefError when I try to run my program from the command line. It works fine in Netbeans, and javac compiles correctly.
I have a class called DistributedSystem which so far is only supposed to print "hello.".
The directory is src/distributedsystem/ which contains DistributedSystem.java, and DistributedSystem.class after the compile.
If I'm inside src/distributedsystem/ and run
java DistributedSystem
then I get
Exception in thread "main" java.lang.NoClassDefFoundError: DistributedSystem (wrong name: distributedsystem/DistributedSystem)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I also get the exact same error if I'm inside src/distributedsystem/ and run
java -cp . DistributedSystem
which is what I thought would fix the problem. I have also tried making sure classpath isn't set somewhere else, even though -cp should overwrite it. Anyone have any ideas what could be wrong?
Go to folder src then compile from there and then run
The classpath should point to the base directory. It looks as if you are attempting to run class DistributedSystem in package distributedsystem, but your classpath is set to project/bin/distributedsystem instead of project/bin.

yet another: Exception in thread "main" java.lang.NoClassDefFoundError

I have a common problem but the common solutions seems don't work for me.
I have a file called test.java in my current directory with others class files in the myclasses directory. Namely if i type
ls:
myclasses test.java
I type:
javac test.java
and all compile fine.
When i try to execute test.class and then type:
java test
i get:
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: myclasses/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: test. Program will exit.
I guess that this means that java can't locate the test.class file. But why? the file is in the current directory and all classes are in the myclasses directory. I have tried different combinations of flags (-cp , -d -sourcepath) and i have moved the test.class around the directories.
Where is the mistake?
Make sure that test.java contains public static void main method.
Also, if your class is inside myclasses, you should java myclasses.test, i believe
In addition to the above, make sure there is no package name (myclasses) declared in your .java file.
You should specify classpath for searching the Class.
java -cp . test

error in java basic test program

$cat JarTest.java
package edu.testjava.simple;
public class JarTest
{
public static void main(String args[])
{
System.out.println("Hello World\n");
}
}
$javac JarTest.java
$java JarTest
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: JarTest (wrong name: edu/testjava/simple/JarTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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: JarTest. Program will exit.
Why so?
Anything wrong with package etc?
Thanks!
You need to do:
$ java -cp . edu.testjava.simple.JarTest
You need to specify the name of the class with the full package name (in this case edu.testjava.simple). You don't need .class or .java on the end.
You can also see the answers to How to execute a java .class from the command line.
Yes, it's because of the package. Java enforces the project structure to be the same as the package structure. That means, that the class in the edu.testjava.simple package must be located in the edu/testjava/simple/ directory.
You may either:
Remove package declaration (this, however, is only acceptable in such "hello world" cases)
Compile the file using javac -d . JarTest.java (this will put the .class file in the appropriate directory) and launch it via java edu.testjava.simple.JarTest

Categories