• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with first java code  RSS feed

 
Alex Frauca
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

My name is Alex and I am interested in learning Java. I bought Head First Java (Second edition) book but I am having some problems and I need your help.

In the book they ask me to type my first source code, compile it and run the compiled bytecode.

1. Source Code

This is the code they ask me to type.

import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} // more code here...
}

I type it in notepad and save as Party.java

2.Compiler

I open a cmd prompt and wirte "javac Party.java". I get a second file called Party.class (If I open this document with notepad, in order to read the code, I see some strange symbols, is it normal?)

3. Virtual Machines

Here it comes my serious problem. The book says " Run the program by starting the Java Virtual Machine (JVM) (Whats that?!) with the Party.class file. The JVM translate the bytecode into sth the underlying platform undesrtands and runs your program."

Well, I dont kno how to execute the Party.class in the JVM because I don't know whats a JVM. I have tried to execute the code in the cmd promp wirting java Party but I get this error: Did not found the principal method in Party class, define the principal method in this way: public static void main(String[] args) (Note that this is a translation of the error message in spanish)

What shoul I do to run this code?

Thank you very much 
 
muhammad rizka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be that you must install jdk first and next you set environment on your PC.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to The Ranch, and no, your problem is not so serious. You're doing great!

You can learn how to post your code in code tags by reading the new user/Forum FAQ. Posting the code in tag tags makes it easier to read and copy (and a bunch of other things).

Somewhere in the book you learned to compile the source code using the 'javac' command at the command prompt. Similarly, you run the compiled code, the *.class file, by using the 'java' command. Note that the command is 'java Party' to open an instance of the JVM and run the bytecode in the Party.class file.

Yes, it's normal for the *.class file to appear as symbols and unreadable characters in a text editor like Notepad. The *.class files are not meant to be read as text files.

You can learn more about the JVM, what it is and how it is important at several places on the web, but you might start by reading this.

Good luck, and come back with questions as often as you need.

Edit: Missed the edit, so yes, a main() method is needed.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Frauca wrote:
Here it comes my serious problem. The book says " Run the program by starting the Java Virtual Machine (JVM) (Whats that?!) with the Party.class file. The JVM translate the bytecode into sth the underlying platform undesrtands and runs your program."

Well, I dont kno how to execute the Party.class in the JVM because I don't know whats a JVM. I have tried to execute the code in the cmd promp wirting java Party but I get this error: Did not found the principal method in Party class, define the principal method in this way: public static void main(String[] args) (Note that this is a translation of the error message in spanish)



Not all java classes are directly executable via the "java" command -- meaning some classes are meant to be called from other classes. Anyway, in order for the JVM (the "java" program) to call the class directly, it needs a main() method, as stated in the error message.

Henry

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch to both of you.

Since Alex can successfully run "javac", it's not the JDK.

Alex the compile bytecode is a "class" file for the JVM to use. The JVM is really a runtime environment for your Java programs.

Did you try running the program by doing "java Party"? ... Wait I think you did that because you got an error say no main blabla.

The fact with this program is that it's incomplete even with a main method. Because if you indeed add in a main ... you run it but no output will be shown.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex, welcome to the Ranch. Just as a heads up, for future posts, can you please UseCodeTags (<- click link)? They make code easier to read.


Alex Frauca wrote:... I get a second file called Party.class (If I open this document with notepad, in order to read the code, I see some strange symbols, is it normal?)

Yes, the .class file is a binary file, it is 'data' not text, and so isn't meant to be human readable (or opened in notepad).

Alex Frauca wrote:The book says " Run the program by starting the Java Virtual Machine (JVM) (Whats that?!) with the Party.class file.

The JVM is an application - it is what you start when you type java Party. The java is an executable, on Windows it is java.exe, and java.exe starts the JVM. So you are doing it right.

Alex Frauca wrote:I get this error: Did not found the principal method in Party class, define the principal method in this way: public static void main(String[] args)


1) Make sure the class Party is public. so it should look like this:

2) Also make sure that you have a method which exactly matches the signature public static void main(String[] args). The method must have the public and static modifiers, must have a void return type, and must be named main, and take a String array (String[] ) parameter.
 
Alex Frauca
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you very much!

So if I undesrtood well, the porblem is that this code is incomplete, and if I want to run it in the cmd prompt writing java Party, I need a main method. Is it correct?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, yes.

When you type:

java <some class name>

Java opens that class file, and looks for a very specific method...it MUST look like this:



now...the "public static void" does not have to be in that exact order, but that is the way 99.999% of people write it, so that's what anyone else reading your code would expect.

the word "args" could really be anything you want - it is just a variable name. But again, 99.999% of the world uses args, and that is what everyone will expect to see.

If your class you call with the "java " command doesn't have that method, looking like that, you will get an error.

 
Satheesh chandragupthan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi you have to enter the library function ,then class,then enter the main class,In body of the main class you have to give the code ,then run it..it will work

Two steps to compile and run
type in command prompt



javac filename.java

java filename

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!