• Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting the Learning Process  RSS feed

 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm so new it's stupid.....

But, I'm going to learn this stuff if it kills me.

I created my first code and saved it to My Documents/Party.java

I compiled it from a command line on my Windows workstation as Pary.class in the same diretory.

Now I'm instructed to run the program by starting the Java Virtual Machine.

Where on earth is this Java Virtual Machine?

Thanks,
Mike
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program called java. The compiler was javac, wasn't it?
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I used Javac in my command line window on my Windows workstation to complile the program called Pary.java. It created the Party.class in the same directory.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point, that may or not have been missed, is that the java command is used to start a JVM and load the program to be executed.

Welcome to the Ranch.

And don't worry about "stupid" questions. Everyone was a newbie at one point.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike

On the CL, type java and nothing else. What output do you receive?

Assuming the command is found, java is the JVM and you use to run your program:

java Party

Note, you do not need to add the .class prefix.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I understand that. Ill run the jvm using the java command at the cl prompt so I can see what result I get. I got a ways to go. Thanks for the input.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Note, you do not need to add the .class prefix.


Suffix.



(Just be aware that the command line gets more complicated than that as soon as you have multiple classes.)
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear, I meant suffix.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning!
OK, I typed java Party at the command line. I got the error, could not find or load main class party.

I did install a path variable per my instructions. I don't know if it is correct or not. Here is what I did with regard to this:

I went to the system in control panel and changed the path to read PATH = c:\Program Files\Java\jdk1.7.0_25/bin I did this in the system variables section.

So, I would have expected to be able to type the command java Party from any directory and the program to run.

Can you help me with what I'm missing?

Thanks!
Mike
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike McGibbony wrote:could not find or load main class party.

Is that literally what it said? Be careful with the spelling. Java is case-sensitive; "party" is not the same thing as "Party". Pass the correct name to 'java'.

Are you in the correct directory (My Documents) when you execute the command? If not, you have to first go there with the 'cd' command. If you're not familiar with the Windows command prompt and basic commands such as 'cd' and 'dir', then I recommend you lookup a tutorial for using the Windows command prompt.

Also have a look at Oracle's Hello World tutorial - it explains step by step how to write, compile and run your first Java program, including the commands to use in the command prompt, and it also has a useful page with common problems and their solutions.

Mike McGibbony wrote:So, I would have expected to be able to type the command java Party from any directory and the program to run.

Setting the PATH makes sure that Windows knows where to find java.exe. But this doesn't mean that Java can automatically find your Party.class file. You have to be in the directory that contains the class file for Java to find it (or the directory has to be added to the classpath).
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I went to the c:\users\mmcgibbony\my documents directory.

At the command line, I typed java Party and hit return.

I got this error.

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

I compiled the source code, Party.java using javac. I did not get an error.

Thanks for the help.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike McGibbony wrote:Error: Main method not found in class Party, please define the main method as: public static void main(String[] args)

That error message tells you exactly what to do. So, do you have a main method in your class Party, and does it look like the error message suggested?

Every Java program has to have a public static void main(String[] args) method somewhere. That's where the program starts when you run it.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I don't understand that .....yet, but here is the sample code from the book that I'm studying. The intent of the exercise is to show me the process to compile rather than to know how to code yet. Page 3/685!

import java.awt.*;
import java.awt.event.*;
class Party {
public void builInvite(){
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 looked to see if I misspelled something but didn't see it.
Thanks!
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did correct a spelling error in buildInvite from builInvite
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK, here is my code that I am trying to run. I go to my command line and cd to the directory where my Party.class resides.

I type java Party at the prompt and I get this error:
Error: Main method not found in class Party, please define the main method as:
public static void main(String[] args)

I think I typed in proper but maybe I am missing something like a space or wrong symbol.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the "more code..." include a main() method? If not, then the error will continue.

For programs started from the command line, a main() method is required as the starting point of the application.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear. That was a great lesson learned with regard to the code tags. I'll study this. I was trying to figure out how to make it look like what I typed in my Notepad ++ editor.

I'll study this and follow the directions you provided too. So, the comment line, // more code here, does that mean something in the future? This came from the book, Head First Java 2nd addition, page 3. I'm just trying to learn the compile process according to the book. I don't know jack about anything right now. My whole goal is to get a good base so that I can advance my skills in Salesforce Apex and Visualforce. This book was suggested as a great start so that is how I am where I am...in the beginning but I have plenty of time to learn....I think.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I used code tags and previewed it. Thanks Bear.
 
Mike McGibbony
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I look at my code that I posted and read where all programs must have a main method. Does this code that I posted, with code tags this time have a main method called



the //more code here... is from the book. I would assume that page 3 of the book would not require me to troubleshoot a program yet
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I THINK that this sample on page 3 isn't meant to be a full-blown program that you can compile and run. I think it is just trying to give you an idea of what a program will look like.

For a command-line program to run, you literally have to have lines like this:



If your file does not have a line like the first one I have, it CANNOT run. period.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!