This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I start the Java Virtual Machine  RSS feed

 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - I am a newbie and I'm following "Head First Java". So I installed JDK 1.5 and added the bin directory to the path etc. I can compile my first piece of code (which, btw, is an example in the book called Party). The instructions in the book say to type the code, compile it and then run on the JVM. OK, code is compiled but no idea how to run on the JVM. The diagram in the book seems to indicate that the JVM should run the Party.class code if I type "java Party" at a command line prompt (I'm using Windows), but when I do that, all I get is "Exception in thread "main" java.lang.NoClassDefFoundError: Party". I'm running the command from the directory in which my class file is located. What am I doing wrong?
[ August 16, 2005: Message edited by: Rita Chakras ]
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you compiled it?

For instance if we use the following code:

public class Party {
public static void main( String[] args ) {
System.out.println("Hello world!");
}
}

save it in a file named Party.java
compile it with the statement

>> javac Party.java

this should create a file named Party.class in
the same directory then run it with

>> java Party

// Lee
 
Eideo Guo
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must config CLASSPATH.if CLASSPATH is not exist,create it!
[ August 16, 2005: Message edited by: Eideo Guo ]
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry a bit quick to read your post there...
as the above post states set the classpath
with:

>> java -classpath <directory path> Party

or run it from the same directory...
 
Reynald Georges
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to avoid another topic since my problem is also the same thing.

OK now that I downloaded Download JDK 5.0 Update 4 the book (Head First Java) and change my path to "C:\Program Files\Java\jdk1.5.0_04\bin", I don't think anything is working for me. Btw I did a search and javac is there. Here's what I typed on Notepad as the book stated:
import java.awt.*;
import java.awt.event.*;
class Party{
public void buildInvite(){
Frame f = new Frame();
Label l = new Label("Party at Reynald's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
}
}


I saved the file w/ the extension ".java". Now I want to know how do I compile it? Should I do it directly from command? I typed "%javac Party.java" but I get an error stating %javac is not recognized as an internal or external command. I try a space between them and nothing either. So I tried w/o % and it says cannot read Party.java

Also how do I get access to the Virtual Machine? The visual interface looks different than mine.

Sorry for all the questions I'm such a newbie
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reynald



The % represents the system prompt or the command prompt for dos and should not be used to compile Java programs. Instead drop the %, and try compiling. That is


After you have compiled the source code you will get a Party.class file. Use the following command to execute(run) your program,


It is also important to make sure your PATH as well as CLASSPATH are set. Check outthis for the same.
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help. Unfortunately, I still cannot run the Party program. I've add the Java bin directory and the directory containing my .java and .class files to the Path and the Classpath System and User environment variables. The program compiles fine, but when I try to run it, I'm getting the error "Exception in thread "main" java.lang.NoSuchMethodError: main", which is actuallt different from the error I received before

Any other pointers? Thanks!
[ August 17, 2005: Message edited by: Rita Chakras ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you've solved the first problem. Now, the JVM can find the class that you're looking for. Now, whenever the JVM starts, it looks for a method named "main" within the class you've told it to load (Party, in this case).

Specifically, you have to have a method signature that looks like:


This is the method that the JVM calls, so it needs to be there with that signature, otherwise the JVM cannot find it. Double-check your Party class to make sure that you have a main method -- and remember, capitalization is important. ("main" is different than "Main")
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Joel.

So I changed the code to add the main and am now able to run "java Party" which is not doing anything. NOt sure if it's supposed to bring up the window as shown in the book or not, because, now the code, with the addition of the main, is different from the example in the book.

BTW... this book was recommended to me as being really good. It's not looking promising, but I'll chug along...
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is the same program that Reynald
has posted try to add the line

f.setVisible( true );

I think this would display the window...

// Lee
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this...



// Lee
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Lee, you're really not a greenhorn, are you?

Writing the code as you've shown did bring up a tiny window, but it's blank.

Anyway, I guess 1 step at a time. I'll wait to rewrite this once I've delved farther into the book.

Thanks, again!
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, didn't see that the buttons and panel
where'nt added... try this:



Havn't coded much java in a couple of years
but as i remember it this should work...

If you want to use the buttons you have to add
a KeyListener, and if you want to change the
layout of the buttons and label look at GridLayout
or BorderLayout (there are more types of Layouts).

Good Luck!

// Lee
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool... thanks! Can't wait to do more fun things like this
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!