1

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

Askar
  • 5,784
  • 10
  • 53
  • 96
  • Where does(folder) it create class file? – Vishal Dec 06 '12 at 09:07
  • The way I try to run it should create .class file in the same directory with .java file – Askar Dec 06 '12 at 09:42
  • Though it's stupid to ask but still want to confirm... Did you create class file using javac? – Vishal Dec 06 '12 at 10:16
  • 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 Dec 06 '12 at 10:28
  • Do u mean to say that you did not run javac? In that case, problem is solved... – Vishal Dec 06 '12 at 10:31
  • Yes, in this case I was trying to compile using java (without javac). Now I'm sure I was wrong. – Askar Dec 06 '12 at 11:10

4 Answers4

1

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
Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • Renaming file name to HelloWorld.java still giving same error. – Askar Dec 06 '12 at 09:03
  • Are you sure you renamed it to HelloWorld.java ? – Bhavik Shah Dec 06 '12 at 09:05
  • did you compile it first 1)javac HelloWorld.java 2)java HelloWorld. Also, check whether you have jdk and jre – Bhavik Shah Dec 06 '12 at 09:19
  • No. As I understand I can use java to compile if there's a main method. And I have it: public static void main(String[] args) – Askar Dec 06 '12 at 09:20
  • javac works fine. The problem is compiling with java. I want to generate HelloWorld.class using "java HelloWorld" – Askar Dec 06 '12 at 09:23
  • No you have to compile it first to create a .class file first run `javac HelloWorld.java` then `java HelloWorld` – Bhavik Shah Dec 06 '12 at 09:23
  • I've read that I can compile and run, if there's a main method with the steps: 1) java HelloWorld . Then it generates HelloWorld.class, which I can run then with 2) java HelloWorld.class. Am I wrong? – Askar Dec 06 '12 at 09:28
  • Bhavik, the name of the .java file has nothing to do with the name of the .class file, which will always be correct. – Marko Topolnik Dec 06 '12 at 09:31
  • The name of the file should be the same as the class name which is public and has the main() method... this is incorrect... See first answer in this post. – Vishal Dec 06 '12 at 10:33
  • Sorry buddy, I think lot of people has answered in the similar fashion. So downvoting your post does not make sense, but due to site's policy I can not remove my downvote until unless u edit ur answer.. – Vishal Dec 06 '12 at 10:55
1

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

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

Laser
  • 6,652
  • 8
  • 54
  • 85
  • Thanks, but it didn't help. I remember Eclipse used to complain that name of a class should be the same as a declared class and with capital letter. javac Test.java works fine. – Askar Dec 06 '12 at 08:58
0

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 {
    ...
}
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110