0

I have the following file ~/workspace/A/A.java:

package A;
public class A{
    public static void main(String[]args){
        System.out.println("Hello world");
    }
}

compiling with ecj don't give me problems,but then I get this error when tring to execute:

~/workspace/A $ java A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
        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)
~/workspace/A $ java -cp . A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
        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)

I tried this on Linux Mint 17 with openjdk-7-jre version 7u55-2.4.7-1ubuntu1

6 Answers 6

3

As you said you have the structure /workspace/A/A.java just open the terminal and browse to workspace directory then type the following command

For compiling

javac A/A.java

For executing

java A.A
Sign up to request clarification or add additional context in comments.

Comments

0

did you try compiling your class first and then executing it

javac A.java

java A

Comments

0

1st you have to compile java program, using below command.

javac A.java

Then, run java A

Comments

0

First you need to compile using javac A.java

then you also need to specify classpath while running like

java -classpath . A

Comments

0

Don't know if ecj differs from javac, so make sure the A.class file has ended up in same dir as your java file (~/workspace/A/) after compiling and try this:

cd ~/workspace
java -cp . A.A

Comments

0

I recommend using a JAVA package name in lowercase especially when your classname is same as the package name. You might want to read this: http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

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.