Solved

Cannot compile JAVA program on Windows 7 PC

Posted on 2014-10-22
7
312 Views
Last Modified: 2014-10-27
When I compile the above program via command prompt: javac HelloWorld.java
The program compiles and I see HelloWorld.class file.
Now when I execute the command on command prompt: java HelloWorld
I get error:
Error: Could not find or load main class HelloWorld

I have installed JDK at the location C:\Program Files\Java\jdk1.8.0_25
and the environment variable JAVA_HOME has been set as C:\Program Files\Java\jdk1.8.0_25


----HelloWorld.java-----------
public class HelloWorld {

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

}
0
Comment
Question by:toooki
  • 4
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40398265
Try

java -cp . HelloWorld

Open in new window

0
 

Author Comment

by:toooki
ID: 40398299
It works, thank you.
Could you kindly let me know how to avoid this "-cp ." every time.
Before I could run using java HelloWorld
Somehow after upgrading to the next Java version, some setup probably got changed.
I added JAVA_HOME and CLASSPATH environment variables. Is there anything else that needs to be setup?

Thank you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40398302
Yes, i would recommend NOT setting CLASSPATH anything other than dynamically
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:rrz
ID: 40399828
If you have a CLASSPATH environment variable defined, then the VM uses the paths listed in the CLASSPATH environment variable rather than searching using its default paths. As a result, if you set this environment variable you need to add the current path otherwise the VM will not find classes in the current working directory.
from http://www.cs.swarthmore.edu/~newhall/unixhelp/debuggingtips_Java.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40399854
Yes. But even if you do include the current directory in the CLASSPATH you can often get nasty results. Fixed classpaths tend to be write once and forget.
0
 

Author Comment

by:toooki
ID: 40405721
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40405917
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 71
varialbe initialization 11 30
Configure a Bean in an XML file 4 15
Error in @AspectJ Based AOP with Spring 2 5
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now