Reputation: 83
Im using fedora 19. Content of HelloWorld.java :
class HelloWorld {
public static void main( String args[] ) {
System.out.println( "Hello World!!" );
}
}
I can successfully compile it using
javac HelloWorld.java
But i cannot run it using
java HelloWorld
It gives the following error
Error: Could not find or load main class HelloWorld
But i can run it using
sudo java HelloWorld
What am I missing here???
Upvotes: 7
Views: 38084
Reputation: 221
go to parent folder and run, it works for me
C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>cd ..
C:\Users\WINDOWS 8\workspace\Demo\target\classes>java dto.HelloHP
Upvotes: 0
Reputation: 61
It seems that your CLASSPATH setting is wrong. check your CLASSPATH and make sure that is :
CLASSPATH="YourJavaHome/lib:."
be attention there is a ':.' in the end of the sentence! after that run
source /etc/environment
and it should be work!
Upvotes: 0
Reputation: 41
I have the same Problem before. Maybe you made the same mistake. My mistake was using "cd" to go inside the package directory rather than the directory right above it. For example wenn the directory right above are called "Hello", you can run it by typing: java Hello/HelloWorld
Upvotes: 0
Reputation: 1
I has the same issue just trying to run HelloWorld on Mac 10.7.5. I compiled the HelloWorld.java file without issue with javac. I then tried to run "java HelloWorld" and got the same error: "Could not find or load main class"
It was only after I changed the directory (cd) in the Mac Terminal to the directory containing the .class file that I was able to run the program.
HTH, Steve
Upvotes: 0
Reputation: 1845
Dear Pranav Chugh,
1- cmd - go the directory of located java file
run the following on cmd
2- javac HelloWorld.java
3- java HelloWorld ---- not not add .class
here you will get the result
Upvotes: 4
Reputation: 14738
You are not setting a classpath that includes your compiled class! java
can't find any classes if you don't tell it where to look.
Try java -cp . HelloWorld
Source here
Not sure why it works with sudo though. My* guess would be, that the CLASSPATH is set for the root user and not for the current user.
Upvotes: 9
Reputation: 718768
This is rather bizarre. It seems like the problem is that when you run java
as a non-privileged user it cannot find or read the ".class" file. But when run as "root", you can.
This suggests that you have somehow managed to create the "HelloWorld.class" file with the wrong owner and/or the wrong permissions.
Check the permissions by running ls -l HelloWorld.class
. The owner should be your user account (not "root") and you need user read permission on the file.
Here are a couple of other possible explanations:
The java
command you are running might not be what you think it is. Check what which java
says when you run it as yourself. Check that it is the "real" java
executable and not some script or something in the current directory or some other directory that won't be on the root / sudo $PATH
.
You might have set your CLASSPATH
environment variable such that the current directory (where "HelloWorld.class" is ... I assume) is not on the classpath. But when you sudo java
, the java
command is running with an environment in which $CLASSPATH
is not set. In that case, if there is no -cp
argument, you will get a default classpath consisting of just "."
; i.e. the current directory.
If the problem turns out to be the CLASSPATH environment variable, I recommend that you unset it ... and edit your shell's "rc" files to unset it there too.
Instead, use the '-cp' command on the java
command, javac
command and so on ... and switch to either Ant or Maven or an IDE for building and running code. (Or you could write some little wrapper scripts as application launchers.)
Don't depends on the CLASSPATH environment variable. It is liable to give you nasty surprises, especially if you are switching between coding projects. (Certainly don't depend on it in your production environment!)
Upvotes: 0