1
votes

I see there're plenty of analogical threads here but they didn't help me.

I've tried to run in Win7 and OSX Mountain Lion: 1) java Test 2) java -cp . Test

//Test.java
class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}

1) for Win7 error:

Error: Could not find or load main class Test

2) OSX Mountain Lion error:

Exception in thread "main" java.lang.NoClassDefFoundError: Test Caused by: java.lang.ClassNotFoundException: Test 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)

Really have no idea what's wrong here... :(

Thanks, Oscar

4
Where does(folder) it create class file? - Vishal
The way I try to run it should create .class file in the same directory with .java file - Askar
Though it's stupid to ask but still want to confirm... Did you create class file using javac? - Vishal
As I already mentioned javac works fine for me. Now I relalised that I was wrong thinking that java could also compile. I posted really stupid question. - Askar
Do u mean to say that you did not run javac? In that case, problem is solved... - Vishal

4 Answers

1
votes

My Test.java:

class HelloWorld {
  public static void main(String[] args) {
    System.out.println("HW");
  }
}

My dialog with bash:

Neoten:bin marko$ ls -al
total 8
drwxr-xr-x   .
drwxr-xr-x   ..
-rw-r--r--   HelloWorld.class
drwxr-xr-x   test
Neoten:bin marko$ java HelloWorld
HW
Neoten:bin marko$ 

You didn't post the contents of the directory where you ran it, so this is the most probable cause of your errors.

1
votes

The name of the file should be the same as the class name which is public and has the main() method. In your case rename the file name from Test.java will compile and will create HelloWorld.class file not Test.class because there is no Test class declared in your file.

after .class file is created , run it with java HelloWorld

To Know More click here and here

so here's an example

//Filename abc.java
public class hi
{
public static void main(String[] args)
{
 System.out.println("Hell");
}
}

the output

abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
       ^
1 error

but if you do this

 //Filename abc.java
    class hi
    {
    public static void main(String[] args)
    {
     System.out.println("Hell");
    }
    }

it will create hi.class file so

D:\>java hi
Hell
0
votes

Looks like your class must have same name as file name.
test.java must contain class test

0
votes

You must have your class name the same as your file, and your classes directory location translatable to your classes package. This is how Java supports namespaces, and having no package declared is called the default namespace - it's not recommended to use the default namespace.

i.e.

// my/name/space/HelloWorld.java
package my.name.space;

public class HelloWorld {
    ...
}