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!)
java
under your user? – MadProgrammer