anil
anil

Reputation: 143

Error: Could not find or load main class hello in Windows 7

i am a new java student i am running a hello world program in command prompt but i am getting a error

  class hello{

public static void main(String agrs[]){
 system.out.println("Hello world");
}

}

this is my hello world program

G:\java>javac hello.java



 G:\java>dir
 Volume in drive G has no label.
 Volume Serial Number is 32DF-BA6B

 Directory of G:\java

14-Sep-13  04:36 PM    <DIR>          .
14-Sep-13  04:36 PM    <DIR>          ..
14-Sep-13  04:36 PM               415 hello.class
14-Sep-13  04:35 PM               100 hello.java
               2 File(s)            515 bytes
               2 Dir(s)  55,645,966,336 bytes free
G:\java>java hello
Error: Could not find or load main class hello

my java path is right

G:\java>path
PATH=G:\Windows\system32;G:\Windows;G:\Windows\System32\Wbem;G:\Windows\System32
\WindowsPowerShell\v1.0\;G:\Program Files\Java\jdk1.7.0_25\bin

but when is use this command then program run .

G:\java>java -classpath . hello
Hello world

i want to ask that why is my program not run normally is any problem in my path setting variable ? i want to run my program normally as

G:\java>java hello

Upvotes: 2

Views: 17160

Answers (3)

adairjun
adairjun

Reputation: 365

sudo vim /etc/profile

Then you add: export CLASSPATH=$CLASSPATH:.

Quit vim, then

source /etc/profile

Upvotes: -1

jokochan
jokochan

Reputation: 21

set on the system variable from :

C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_45\bin

to

.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_45\bin

Upvotes: 2

Pratik Shelar
Pratik Shelar

Reputation: 3212

You need to set the classpath variable as well. Currently you have just set your PATH variable

1)Main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand CLASSPATH environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in .class file.

2) In order to set PATH in Java you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directory where you have put either your .class file or JAR file which is required by your Java application.

3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.

Upvotes: 10

Related Questions