• Post Reply Bookmark Topic Watch Topic
  • New Topic

error:could not find or load main class  RSS feed

 
Twinkle Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have permanently set the path of bin of JDK 1.6 in the environment variables of my system. I am able to compile any of my java programs successfully. But when I give command to run the program I am getting an error "could not find or load main class".
When I write the command "set classpath=.;" program runs successfully.
I want to know whether I have to set this classpath everytime I run a new progaram. Is there any permanent way to set classpath ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read our FAQ on HowToSetTheClasspath?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't set the classpath environment variable. It just leads to problems as you are seeing. Make sure the folder containing the javac.exe compiler is in the system path variable and when you compile make the folder containing your .java files the current folder. Use the -c compiler switch to point to any external jar files needed.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can set the CLASSPATH environment variable permanently, in the same way as you set the PATH variable.

However: It is better to not set the CLASSPATH environment variable at all. If it is not set, Java will use the current directory (".") as the default setting. You don't see that happening on your system, most likely because CLASSPATH is already set to something. It's best to make sure that it's not set at all.

If you need a specific classpath for compiling and/or running your program, you can also use the -cp or -classpath option of the javac and java tools. For example:

java -cp somelib.jar;. com.mypackage.MyProgram
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Use the -c compiler switch to point to any external jar files needed.

-cp compiler switch
 
Twinkle Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was some problem during installation of JDK.
Now it is working properly.
Thanks to all
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!