0

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.

3
  • 1
    I assume you did properly compile it first with the "javac" command? Commented Oct 31, 2013 at 8:47
  • Simple - javac Assignment02Q01Sec01.java and then java Assignment02Q01Sec01 Commented Oct 31, 2013 at 8:48
  • @Voidpaw - I think he did compile it with eclipse. So the answer from David Wallace, Kayaman, Vivek is correct Commented Oct 31, 2013 at 8:51

4 Answers 4

2
cd \Users\ghostcow\workspace\hw\bin\
java hw.Assignment02Q01Sec01
Sign up to request clarification or add additional context in comments.

Comments

1

You need to run it from bin, not bin\hw as java hw.Assignment02Q01Sec01.

Comments

0

You have to run it as

java hw.Assignment02Q01Sec01

You have to provide the fully qualified class name and run from bin folder.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.