This is pretty weird. It seems that the problem is that when starting java as an unprotected user, he cannot find or read the ".class" file. But when running as "root" you can.
This suggests that you somehow managed to create the HelloWorld.class file with the wrong owner and / or wrong permissions.
Check permissions by running ls -l HelloWorld.class . The owner must be your user account (not "root"), and you need to get user permission on the file.
Here are a few other possible explanations:
The java command you run may not be what you think. Check what which java says when you run it as your own. Make sure that it is a βrealβ java executable, and not some script or something in the current directory or another directory that will not be in the root directory / sudo $PATH .
You may have set the CLASSPATH environment variable so that the current directory (where "HelloWorld.class" ... I suppose) is not in the classpath. But when you sudo java , the java command works with an environment in which $CLASSPATH not set. In this case, if there is no -cp argument, you will get a default path consisting only of "." ; i.e. the current directory.
If the problem turns out to be a CLASSPATH environment variable, I recommend that you cancel it ... and edit the "rc" shell files to disable it too.
Instead, use the β-cpβ command in the java command, the javac command, etc .... and switch to Ant or Maven or the IDE to create and run the code. (Or you could write some small shell scripts as launcher applications.)
Independent of the CLASSPATH environment variable. It can give you unpleasant surprises, especially if you switch between coding projects. (Of course, do not depend on this in your production environment!)
source share