aspose file tools*
The moose likes Java in General and the fly likes run time error: could not find or load main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "run time error: could not find or load main class" Watch "run time error: could not find or load main class" New topic
Author

run time error: could not find or load main class

Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
Hi! I am new to Java.I tried the HelloWorld program.Using javac command,I could compile it.A .class file got created.But,when I tried running the same with java command, an error-"could not find or load main class HelloWorld" is displayed. I have checked the spelling, as I know java is case sensitive.
Please help me out.
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
I would like to add one more thing that I am using WINDOWS7. I have set both the user variable name and system variable name as" CLASSPATH " in Environmental variables,with path address(for both) as- C:\Java\jdk1.7.0\bin. Is the error related to this path setting?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome to the Ranch

I hope you tried like below

java HelloWorld

and if the HelloWorld.class file is in different location say C:/MyClasses then try,

java -cp C:/MyClasses HelloWorld

where -cp specifies the classpath ( location of the .class files)
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
Thank you
The .class file and the .java flies are n the same folder.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Could you please post the code of HelloWorld.

Also try java -cp . HelloWorld
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
With java-cp.HelloWorld I get the following error

Unrecognized option:-cp.HelloWorld
Error: Could not create the java virtual machine.
Error: A fatal exception has occurred.Program will exit.

yah...m pasting the program..

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
There should be a space in the command you specify.

java[space]-cp[space].[space]HelloWorld

and please UseCodeTags henceforth
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
Thank you. I got the output--Hello world!.
Do I need to type java -cp . <class_name> every time I run a prog? Wont the command java <classname> simply work??? Is the CLASSPATH setting correct???
Will you please clarify how to use compile a prog without going into the bin...I mean without typing the complete path address (C:\Java\jdk1.7.0\bin) again and again.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You are welcome...

Two things i can think of not using the -cp option is
1. If your .class file is in the same directory then you don't have to specify the class path option
2. If your .class file is present in the subfolders say C:/MyClasses/com/mypackage/HelloWorld.class and com and mypackage are the packages in which the HelloWorld is present. Now say if you are in the location C:/MyClasses in the cmd prompt you can just give java com.mypackage.HelloWorld
I hope you knew about packages - the declaration you give at the start of a java program if needed - say in this case ->

You do not need to specify every time the path of the java.exe. To make the OS to locate the java exe it you have to specify the JAVA_HOME and path variables in the Environment variables.

This post should help you setting the environment variables - http://www.coderanch.com/t/551943/java/java/Wrong-version-appearing-java-command
and this to understand path & classpath -> http://download.oracle.com/javase/tutorial/essential/environment/paths.html

Also I think Java should be already installed in your OS by default. So just type the command java in your command prompt and see what the output is shown.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Did you set a system classpath yourself? If so, and there is nothing in the classpath added by anything else, you are probably better off deleting it altogether. System and user classpaths usually do more harm than good.

If the classpath was set by another application, edit it by adding the . at the beginning. It will read something like .;C:\something\folder;C:\Program Files\qt\lib;... Quicktime is one of the applications prone to setting a system classpath.
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
Thank you for your assistance.
I have set the path as suggested and now I compile the program by simply typing javac <classname>.java

Mr. John, the output which I got by typing java in the command prompt is--


















































John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay the output tells java exe is located fine.

but double check whether it is using the version you intend to run your classes using the command java -version
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
The output I got by typing java -version is as follows:-



I have installed JDK7 and I am unaware of the meaning of the last line.I am really grateful for you have taken so much of pain to solve my problem. Thanks a lot.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Saptaparna,

My knowledge is restricted to Java 1.5.... and it would be appropriate for other ranchers who knew 1.7 version to answer your query... hope so you will get that in a moment..

And please start new thread for new questions...
Saptaparna Dhar
Greenhorn

Joined: Sep 16, 2011
Posts: 10
Thank you once again Mr.John. The technical chat was really interesting and helpful.Thanks for your assistance.

Have a nice time.See you again.

Good luck!

With regards,
Saptaparna
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Sorry, I found the problem I forgot to write out the entire thing: java -cp . Test. So it does work now. disregard the post.

Saptaparna Dhar wrote:Thank you for your assistance.
I have set the path as suggested and now I compile the program by simply typing javac <classname>.java

Mr. John, the output which I got by typing java in the command prompt is--



I have a related issue. I am getting the above message and I have entered the command:

Why does this screen appear in the windows command prompt and what can I do to run my program. I created a simple hello world program and used:

and I got the output Hello world, so it must have executed fine actually the same thing is happening to my hello world program too. I do also have a jar file that I needed for jsch-0.1.48.jar in the same folder as the java and .class files. I'd Also like to mention that I ran this in eclipse and it works just fine. I've never used the command prompt to execute java before. Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Have you never used the command line before?
If you need to write -cp . you have a classpath problem. You ought not to set a system classpath; if you set your own, you ought to delete it. If it was there before you started, eg containing QuickTime entries, add a . for current directory at its beginning. In Windows® it looks like this .;blahblahblah and on other OSs like this .:somethingsomethingsomething. You probably have to reopen the command line to see any effect.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
hamza mohamed ansari,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run time error: could not find or load main class