How to run a java .class file?

BlitzMax Forums/BlitzMax Programming/How to run a java .class file?

William Drescher(Posted 2010) [#1]
Okay, so from my BlitzMax program, I can successfully compile a java file into a class file, but when I try to run the class file using the java command, it gives me this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: /Users/Will/Java Files/JavaFileTest
Caused by: java.lang.ClassNotFoundException: .Users.Will.Java Files.JavaFileTest
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

Does anyone know what may be causing this error?

Also, the class file runs perfectly fine from a terminal.


plash(Posted 2010) [#2]
Are you running the same exact command from your terminal as in your application? Are you sure you're in the correct path? Does Java need to know the paths of other classes/.jars?


William Drescher(Posted 2010) [#3]
From the terminal, I just cd into the folder with my .java files and run the command
javac JavaFileName.java
A "JavaFileName.class" file is produced. Then I run
java JavaFileName
This runs the java class code. You don't need the .class on the end when you use the "java" command. In blitzmax, I use
system_("javac /MyJavaFiles/NewJavaFile.java")
this produces a .class file that can be run from a terminal perfectly fine. Using the command in BlitzMax
system_("java /MyJavaFiles/NewJavaFile")
I get the exception error above.


jpavel(Posted 2010) [#4]
If you look at this line in the stacktrace,

java.lang.ClassNotFoundException: .Users.Will.Java Files.JavaFileTest

you notice a dot before Users, so it seems you've accidentally prepended a period to your full classname.

Also, with the java command, you're supposed to use a fully qualified -class name-, not a file name, as its parameter. So it's somewhat hackish that it runs from the terminal too.

Regards,
Jesse


William Drescher(Posted 2010) [#5]
The dots are actually slashes, i'm not sure why they were changed when I pasted the exception. This is running on a Mac. The class name is "NewJavaFile". In the code, it's
public class NewJavaFile


I'm confused mostly as to why it compiles correctly in the same environment, but doesn't run. All of the syntax for both commands are the same.

Edit: I went back and looked at the output some more. I'm not sure where those dots are coming from now that I really look at it. It seems like it may be changing the slashes into dots in the JRE but i'm not 100% sure on that point.


Galaxy613(Posted 2010) [#6]
Have you tried making it so the slashes aren't an issue at all and just put the .java file in the same directory just to test?


William Drescher(Posted 2010) [#7]
I tried that, all it did was return a dot in front of the file name. I also tried placing it in the root folder of my hard drive ("/") to no avail. I'm curious: is it because BlitzMax takes all the output and pipes it into the BlitzMax "Output" window? If I run the program outside the IDE, the "Print" command won't open a terminal by default. Is this an issue only seen on a Mac?


Redspark(Posted 2010) [#8]
The execute statement that you typed manually is not the same as the one you used in Blitz. Also, if your class is on the classpath and Blitz has that variable within its environment, then it should see it. However, depending on how Blitz creates a shell for the program to be executed in, it may not be setting the environment variable.

Try getting Blitz to print the environment variable: CLASSPATH.


William Drescher(Posted 2010) [#9]
CLASSPATH amounts to a blank string. So how would I set CLASSPATH correctly?


Redspark(Posted 2010) [#10]
You have to set your environment up before you execute java. I don't know how that is done in Blitz. Blitz may have the ability to set its own environment or you could try creating an sh file to set the environment and call java for you. That's not the best way but it may be a good workaround until you find out how to do it properly.

Just type in the commands that you would used at the terminal prompt and then call the .sh file instead of the java command. (Something like system_("sh batchfile.sh"); )

Last edited 2010


William Drescher(Posted 2010) [#11]
Well I'm mostly a novice at Unix terminals, so what would the command be to set an environment variable directly from a terminal window?


Redspark(Posted 2010) [#12]
I'm no expert either but it should look something similar to:

CLASSPATH=<Your PATH>
export CLASSPATH

Use the command 'set' to list all of you environment variables in terminal.

Use 'man' to get help on any terminal commands. 'man -k <word>' does keyword searches, if that helps any.


William Drescher(Posted 2010) [#13]
Okay, well I just found the world's craziest workaround for this little issue. I figured that if you can use "&&" in windows to tack on extra commands to one line in a batch file, the you could use ";" to do it in a Unix terminal. Well I was right (hurray for me). So this is how I got the java file to run.

system_("cd " + Replace(ExtractDir(saveFileName), " ", "\ ") + "; java " + StripAll(saveFileName))
And viola! It worked perfectly. Thanks to all who helped me learn these rules (and tricks).


Redspark(Posted 2010) [#14]
Excellent! Good to know.