Compiling

HELP ME!



I want a sample statement that you type in command prompt to compile the .java file. There is a sample statement in wiki that would run the .class files but there is no mention of the statement that compiles the java files. Please help me and please include all the required libraries (jar files) that are required to compile the source file and anything else that is required.

ant compile

if you use an IDE like eclipse or netbeans, you would not need to compile anything manually it does it for you.

I know that, but still I prefer the command line. It makes things a lot simpler.

That's an inherent contradiction.

Here's a simple statement:

javac path/to/file.java



This assumes default class paths, output paths, etc. It also assumes that jdk/bin is in your PATH. You can look at the documentation for javac to see how you can change the class paths, libraries, outputs, etc.

I used the following command to compile "HelloWorld.java":



javac -cp ./lib/lwjgl.jar;./lib/jogg-0.0.5.jar;./lib/jorbis-0.0.12.jar;./target/jme.jar;./target/jme-awt.jar;./target/jme-effects.jar;./target/jme-model.jar;./target/jme-sound.jar;./target/jme-terrain.jar;./target/jmetest.jar;./target/jmetest-data.jar HelloWorld.java



It was properly compiled. Than I used the following to run the "HelloWorld":



java -Djava.library.path=./lib -cp

./lib/lwjgl.jar;./lib/jogg-0.0.5.jar;./lib/jorbis-0.0.12.jar;./target/jme.jar;./target/jme-awt.jar;./target/jme-effects.jar;./target/jme-model.jar;./target/jme-sound.jar;./target/jme-terrain.jar;./target/jmetest.jar;./target/jmetest-data.jar HelloWorld



but it gave me a "Exception in the main class". I am quite sure that my code was correct because I:

1: Copied the code from wiki tutorial and got the same error.

2: Copied the code from downloaded tutorial and got the same error.

3: Used the file …jmesrcjmetestTutorialGuideHelloWorld.java (The one I compiled earlier by "ant dest-all" command) and got the same error.



Can anyone point me my mistake, is that the application is not properly compiled?

mastermindsheikh said:

HELP ME!

I want a sample statement that you type in command prompt to compile the .java file. There is a sample statement in wiki that would run the .class files but there is no mention of the statement that compiles the java files. Please help me and please include all the required libraries (jar files) that are required to compile the source file and anything else that is required.

Two words :e-clipse !

The sooner that you understand that eclipse rules the world, the better.

Command line is fun too though.

I just happen to witness an interesting phenomenon that made me understand why I am continuously getting the same error:



"Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld"



I erraneously attempted to run HelloWorld.class through the command line when I even didn't had the source code HelloWorld.java compiled into HelloWorld.class (I had scraped all previous compiled versions). And I got the same error!



Doesn't it mean that somehow my HelloWorld.class files are not being found by the java environment (They are working properly when I run the jmetest.TutorialGuide.HelloWorld). Now the new problem is, how can I make them known to JAVA???

Well do you remember to include the package name ?

LIke java -cp (ur classpath) mypackagename.HelloWorld ?

You also have to be in the root of the package dir for it to work, not in the same dir as you classes unless

your classes has no package name.

Baune! Could you guide me step by step so that all those who read this post may be benefited (including me).



I suppose I've compiled the HelloWorld.java into HelloWorld.class. This HelloWorld.java is a copy of jme/src/jmetest/TutorialGuide/HelloWorld.java



The command I used to compile is:



javac -cp ./lib/lwjgl.jar;./lib/jogg-0.0.5.jar;./lib/jorbis-0.0.12.jar;./target/jme.jar;./target/jme-awt.jar;./target/jme-effects.jar;./target/jme-model.jar;./target/jme-sound.jar;./target/jme-terrain.jar;./target/jmetest.jar;./target/jmetest-data.jar HelloWorld.java



and it is properly compiled. Now I want to run it. All I have got is a HelloWorld.class file. Please guide me step by step from this ownwards…


I think it's good to know how command line works but it's not really used that much. I'd feel kind

of silly if I didnt know how it worked but only knew how to work with IDE's though so imho you have to learn it.

I've seen many java developers who knows all about EJB and BEA and 10000 of buzzwords but can't use the command line.

Then I just silenty shake my head…

BTW, I'm happy for these kind of questions because here I can actually answer something :slight_smile:



You have to understand the package structure of java. A class can have a package name as well as a name.

So if the class has the package name net.tests.mytest  it must be put in

a directory net/tests/mytest when you use command line you have to create this structure yourself

which is one of the reasons that bar none uses command line except for fun and to know what it is.

So the name of the class is not only MyClassName since many classes could have that name, but packagename.classname, example net.tests.mytest.HelloWorld.

It's a "fully qualified name", it is supposed to be a unique name in the world. That way you can have ur HelloWorld and other people can

have their HelloWorld and the compiler and Java can distinguish  between them.





When you want to run the class be whereever you want, but set classpath like this

java -cp (path to ROOT, meaning the super directory of net/tests/mytests);(all ur jars)  HelloWorld (class name)

So example if your class is compiled to

c:/myprojects/classes/net/test/mytest/HelloWorld

Your java command should be (I dont know where u put ur libs u have to make sure u point to the right dir there as well)

java -Djava.library.path=./lib -cp c:/myprojects/classes/

./lib/lwjgl.jar;./lib/jogg-0.0.5.jar;./lib/jorbis-0.0.12.jar;./target/jme.jar;./target/jme-awt.jar;./target/jme-effects.jar;./target/jme-model.jar;./target/jme-sound.jar;./target/jme-terrain.jar;./target/jmetest.jar;./target/jmetest-data.jar net.tests.mytest.HelloWorld



So in essense :  java -Djava.library.path=path_to_ur_libs  -cp path_to_ur_classes;path_to_jar1;path_to_jar2;path_to_jar3 FULLYQUALIFIEDNameOfClass__RememberPackageName



Note that you point ur classpath to the ROOT of all ur classes, not to the class itself.



When u understand all this, it's good and u can forget about it again and just use eclipse :slight_smile:



Hope it's clear.




I agree in full that the command line is a valuable tool that should not be overlooked when developing any software (and should be used by everyone not just programmers). But using the command line on a project like jME is like using a framing hammer to fix a car, it's just not the right tool.



(That is unless you work for a shady autobody shop ;))

Back to topic, I think he is missing the . (current) directory in his classpath, and thus, java is not finding his .class file.

Thanks Baune. You pointed out correctly and now everything is going out fine. I was really missing that package thingy.