I'm following the java tutorials on the Princeton website .
I'm running debian sqeeze 64bit
and I have installed Sun java version 6.
I can compile and run the basic hello world
program without any problem, using the terminal and the Eclipse IDE.
The problem is:
when I try to compile and run a program, which requires an argument input for example:
public class RandomSeq {
public static void main(String[] args) {
// command-line argument
int N = Integer.parseInt(args[0]);
// generate and print N numbers between 0 and 1
for (int i = 0; i < N; i++) {
System.out.println(Math.random());
}
}
}
I can run this on Eclipse putting an integer argument, however it doesn't work on the terminal.
I get this error:
emes@debian:~/Documents/workspace/IOput/src/randomSeq$ java RandomSeq 21 Exception in thread "main" java.lang.NoClassDefFoundError: RandomSeq (wrong name: randomSeq/RandomSeq) 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: RandomSeq. Program will exit.
I've tried to update the /etc/profile
to include the java-6-sun
on the PATH variable.
I'm not sure, what to try from here.
Apparently, you're trying to run your program from the src
folder of an Eclipse project. src
stands for “source”. The executable version of your program (the compiled classes) is not in src
; it's in bin
, which stands for “binary”, ie machine code.
When using the command line, you should first compile your program:
javac MyClass.java
and then run it:
java MyClass
But please, do not do it inside an Eclipse project's directory structure, or you will create additional files (class files) not expected by Eclipse at this location.
Additionally maybe you are inside a package. You can't run a class if you're within its package folder. You need to be at the top-level of the package hierarchy.
Example: suppose your class is inside a package named mypackage
. Then in someFolder/mypackage/MyClass.java
you will have something like:
package mypackage;
class MyClass {
...
}
After compiling your code, you must be in somefolder
and issue the shell command:
java mypackage.MyClass
It looks as if your class has a package
package randomSeq;
public class RandomSeq {
If so, then when starting it it should be located in the folder randomSeq and the root of that folder should be in your class path and the package must be specified when invoking.
So, if your .class file is in bin/randomSeq then you could run it with java -cp bin randomSeq.RandomSeq 21
Don't bother about the argument as that would give a run-time null pointer exception.
The problem is your classpath.
Make a list (ls or dir) in the directory you run java RandomSeq from. Do you have a .class file
there. If not run javac RandomSeq.java
first to generate the class file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.