0

every time while i try to run java program in command prompt, it shows "could not find or load the main class.

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

javac -d ../classes A.java compiles successfully

then try to run in classes folder

java A

i got the message "couldn't find or load main class A"

shahim
  • 1
  • That shouldn't be required. `echo 'class a{public static void main(String[]_){System.exit(42);}}'>a.java&& javac a.java&&java a;echo $?`. `42` – Vlad Dec 13 '13 at 12:27
  • Is that the entire file? Just in case, if you have a `package foo` statement at the top you need `java foo.A` instead. – Vlad Dec 13 '13 at 12:30
  • my all programs gives the same message. i could ran these programs while ago without the classpath – shahim Dec 13 '13 at 12:44
  • @shahim more than likely you were not outputting the class files to the `classes` folder – Reimeus Dec 13 '13 at 12:49
  • @Vlad yes. no package – shahim Dec 13 '13 at 12:57

2 Answers2

3

Add the classes folder to the classpath

java -cp ../classes A
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • OP says they are in the "classes" folder. You don't need to add the current folder to the class path. This answer won't make the slightest bit of difference. -1. – Dawood ibn Kareem Dec 13 '13 at 12:25
  • need it to apply for every programs? my all programs gives the same message. i could ran these programs while ago without the classpath – shahim Dec 13 '13 at 12:35
  • Yes, if you want to run the application in a directory other than the `classes` folder – Reimeus Dec 13 '13 at 12:37
  • @DavidWallace yes it will make a difference in the 1st scenario where the `OP` is not located in the `classes` folder – Reimeus Dec 13 '13 at 12:51
  • but i telling that i could run these programs in the same way what i did. but now i can't. – shahim Dec 13 '13 at 12:54
  • As I said above, you were probably dumping the classes into the same location as the source files, now they are in the `classes` folder so you need to add this to the classpath – Reimeus Dec 13 '13 at 13:01
0

You are compiling with -d option .So package structure will be created .If your class is packaged then you have to use fully quallified class name means outerpackage.innerpackge.A

Deepak
  • 2,287
  • 1
  • 23
  • 30