Reputation: 3
Hey I wrote a simple program in Eclipse:
package hw;
public class Assignment02Q01Sec01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length == 0) {
System.out.println("No arguments!");
return;
}
System.out.println(args[args.length - 1].charAt(args[args.length -1].length() - 1));
}
}
It runs fine when selecting the 'Run' menu in Eclipse but fails when running from command line:
c:\Users\ghostcow\workspace\hw\bin\hw>java Assignment02Q01Sec01
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment02Q01Sec01
(wrong name: hw/Assignment02Q01Sec01)
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)
What am I doing wrong?
NOTE: I'm in my class path in cmd, and '.' is included in the CLASSPATH env variable, i checked.
EDIT: Thanks, problem solved.
Upvotes: 0
Views: 239
Reputation: 113
Java program must be run with qualified name of the main class. In this situation, it should be "java hw.Assignment02Q01Sec01 directly outside directory hw(compiled class, not source code)
Upvotes: 0
Reputation: 1963
You have to run it as
java hw.Assignment02Q01Sec01
You have to provide the fully qualified class name and run from bin folder.
Upvotes: 0
Reputation: 73558
You need to run it from bin, not bin\hw as java hw.Assignment02Q01Sec01
.
Upvotes: 1
Reputation: 79838
cd \Users\ghostcow\workspace\hw\bin\
java hw.Assignment02Q01Sec01
Upvotes: 2