• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I run a class?  RSS feed

 
Tom Chou
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OS: OSX Mavericks
Java Version: 1.7.0_45

Book: Head Start Java

Pg. 3

I've got the paths set. (I think, since i was able to type in javac on a .java and get a class to come out without errors).
I've got Party.class compiled.

On page 3, it says run it using Java Virtual Machine. So i tried typing Java Party.class in terminal, get an error "could not find or load main class Party.class". I also tried double clicking in finder, and it said to look in console for error message and nothing ran. Am I supposed to see something like the picture on Pg. 3?

How do I run this Party.class thing?

Thanks,
Ultra-noob
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

$ java Party
 
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
Welcome to the Ranch!

Type:

java Party

not Java (it's case-senstive), and don't include the .class

[Edit: dang, Junilu beat me to it by 20 seconds! ]
 
Tom Chou
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i'm following the book and i have this in the .java file.




but i'm getting an error after typing "java Party":

Main method not found in class Party, please define the main method as:
public static void main(String[] args)

??
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need main method to run a class. Please add main method and create an object of Party and call a method of it, and rerun your program.
 
Tom Chou
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm going to have to read ahead and figure this out.

Why would they write the initial tutorial like this and not have it include the necessary code to run right? ugh. -_-'
 
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
I think you are putting the cart ahead of the horse. Read on. It's clear that the Party class is only one part of an AWT (GUI) program. It can't be executed standalone.

Just adding a main method to that one class isn't going to do anything constructive.

But to summarize: the entry point of a Java program is a main() method in one of the classes that comprise the program.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the Note at the bottom left of Page 3 that says "(Note: This is not meant to be a tutorial... you'll be writing real code in a moment but for now, we just want you to get a feel for how it all fits together)" (emphasis by me)

In other words, it's saying "Don't try to actually do anything yet, we're just giving you an idea of the overall process." Just read and understand. Wait until Page 9 to actually write, compile, and run your first program.
 
Tom Chou
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm.

You know, I did see that and I read it and understood it to be "don't worry about what it all means" and assumed if I typed it in that it would still work. I figured it would also test if i had my paths set right. I mean under the first picture it said "type your code" and even said save so I thought it was supposed to be a quick and dirty for testing or something. oops.

=/

In hindsight, your interpretation makes more sense with the "this is not meant to be a tutorial" emphasized haha.

Thanks for all the help everyone. I guess I will be moving ahead with the reading.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people have that problem. I don't think it is clear enough that Party is incomplete.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!