• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

run time error: could not find or load main class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
The .class file and the .java flies are n the same folder.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please post the code of HelloWorld.

Also try java -cp . HelloWorld
 
Saptaparna Dhar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be a space in the command you specify.

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

and please UseCodeTags henceforth
 
Saptaparna Dhar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 - https://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.
 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hamza mohamed ansari,
Your post was moved to a new topic.
 
Get me the mayor's office! I need to tell her about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic