Java Compilation Problems... :/

BGM

Limp Gawd
Joined
Jul 6, 2001
Messages
456
Hello,

I am just starting to branch out into using different classes with my java as at uni i have to use a couple of special classes for two of my modules..

ok, so iv done this work in the labs at uni, so i know the files im trying to compile do actually compile and run.. however im having such a hard time getting them to 'see' the librarys, .jar files at home.

Last year i just used emacs and cmd compilation, this year we are using net beans.. so i run netbeans (3.6) and first hurdle.. my old programs that compiled and ran at the cmd line now dont work in netbeans... what the hell!? Compiles, builds, just wont execute.. in a simple example iv been testing it on hello world and it just throws the error

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

same exact code compiles and runs at cmd, so i think iv got a netbeans setting wrong.. ( i hate it anyway)

So iv decided to scrap netbeans! Am now just going to work with a txt editor.. its not like i do any level of development that needs a tool like that anyway..

my question is, how do i set up my machine so that the programs i compile at the comand line can automatically find the new .jar files? I have my classpath set and so i tried putting the files in C:\Program Files\j2sdk1.4.2_04\bin but that didnt work..

do i need to change or add soemthing to my environmental variables again ? (win XP)

alternatively, if in your wisdom u believe it a good idea for continued netbeans use? please advise me on how to get rid of this silly error.. im pretty sure its just an option somewhere, but im very unfamiliar with netbeans :)

cheers for reading all that

:D

Chris
 
That's one of my frustrations with most IDEs I've ever tried to use: Sometimes they just do more than you want them to, like managing the classpath, and it adds a layer of frustration because of it.

That said, getting your classpath right can be tricky even without an IDE obscuring it. Exactly how you want to manage your class files (remember, where you put your source makes absolutely no difference) is rather dependent on your project structure. Are you just using a flat structure, or will you be using packages to organize your classes?

If you've just got a flat set of files, it's probably not worth learning Ant or something like that. What I do in those cases is just make a directory to hold hold my classes and do a "javac -d classes *.java" from the directory my source is in. Then I add "classes" to my classpath, either just by typing it in at the commandline or by specifying it in my environment (bashrc or Windows "environment variables" control panel thing). So then I can just type "java MainClass" when I'm in that directory at a command prompt.

If you're getting into packages, you can probably still work in that framework, but I wouldn't recommend it. Give Ant a try for compilation. Then you can use scripts like these to run your application. (See here for an idea of how I structured my last large project, if you want.)

Hope that helps! I can probably provide more specific advice if you give more details about the size of your project, the OS you're developing on, and so forth.
 
Ah, you did say you're on WinXP. Are you running Cygwin or are you just trying to use the built-in command prompt? What are you using to turn your collection of class files into a jar? (Presumably just using the "jar" command, but I want to make sure you're not doing something more complicated.) Does doing "java -jar foo.jar MainClass" work? The nice thing about using jars is that you don't have to set any special classpath; you just specify the jar file on the commandline.
 
thanks for the replys, ill try to answer all of the questions..

funilly enough i installed cygwin the other day, but for an unrelated thing.. i removed it again as it wasnt what i needed.. do u think this could cause problems? im just using the built in cmd prompt now

so i have 4 files all of which are .jar and yes i unpacked them just by using the jar command, that java -jar -foo.jar MainClass didnt work, it just gave an error saying it didnt recognise what -jar was :(

yes my file structure is flat, nothing complicated going on here, i literally just need to do something with the .jars, and probably windows environment variables that lets me easily compile just by using the javac command

a little guide on how to do that under win xp would be great cos i really dont have a clue, lol :)

cheers
 
Back
Top