• Post Reply Bookmark Topic Watch Topic
  • New Topic

JVM? Beginning question  RSS feed

 
Scott Walker
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi im still just starting out.....ive managed to get my code to compile correctly and am left with a class file.....how do i get this to run? Where is the JVM? Im using mac osx 10.4. Probably a stupid question and im sorry to keep bothering you but im so close to being well on my way here lol.....thanks for looking
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this isn't really Mac-specific, I've moved it to the Java in General (beginners) forum where it'll get more air-time.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're currently dealing with only a single class file, running it (assuming it's got the appropriate main() method) should be as simple as:

java classname
 
Scott Walker
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much...i have it working now......it was my shocking code that was wrong lol....i knew it would be something simple like that.....thanks again for taking the time to help someone at such an early stage :0)
 
Ed Arszyla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If you're currently dealing with only a single class file, running it (assuming it's got the appropriate main() method) should be as simple as:

java classname


But what happens if you have more than one class file? Say that I have a test class named Test.class and another class that Test.class uses called Class1.class.

I did this from the Terminal command line in OS X:

Complied Class1.java first, then:

javac <path>/Test.java -classpath <path>/Class1.class

This created a class file called Test.class that could be run by double-clicking it as long as Class1.class was also in the same directory.

The problem is if I want to run Test.class at the comand line instead of by double-clicking it.

If I do this:

java <path>/Test.class

or this:

java <path>/Test.class -classpath <path>/Class1.class

I get an error like this:

Exception in thread "main" java.lang.NoClassDefFoundError

I assume this means that Test.class can't find the Class1.class it needs to run.

What is the solution for this? Do I need to hard-code the classpath someplace?

[ November 05, 2006: Message edited by: Ed Arszyla ]

[ November 05, 2006: Message edited by: Ed Arszyla ]
[ November 05, 2006: Message edited by: Ed Arszyla ]
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complied Class1.java first

That's not necessary.

Class1.class was also in the same directory.



Then compile using the command:

directory containing Test.java> javac -classpath . Test.java

which shows javac the path to the classes referred to by Test.java (a "." signifies the current directory). You don't have to specify the path to Test.java itself, so if the files were in different directories, the -classpath would only specify the path to Class1.java.

The problem is if I want to run Test.class at the comand line:


When you use the java command, you have to specify all the paths to all the classes used in your program:

directory containing Test.java> javac -classpath . Test

In this case, there is only one path specified in the classpath because all the classes in the program are in the same directory. If Class1.class and Test.class were in different directories, you would have to specify the paths to both in the -classpath option.

Do I need to hard-code the classpath someplace?
Never do that. If you want to learn what's going on always specify the -classpath option. If you don't think you need to list any classpaths, then use -classpath "". If you don't use the -classpath option in your javac or java commands, then javac and java will go look in the CLASSPATH environment variable for paths, which will have you thinking no path was needed when a path in there might have been required--completely screwing up your thinking on how things work. When you include the -classpath option, it overrides anything in the CLASSPATH environment variable.

[ November 05, 2006: Message edited by: sven studde ]

[ November 05, 2006: Message edited by: sven studde ]

[ November 05, 2006: Message edited by: sven studde ]

[ November 05, 2006: Message edited by: sven studde ]
[ November 05, 2006: Message edited by: sven studde ]
 
Ed Arszyla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!