wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Running My First Java  Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running My First Java  Application" Watch "Running My First Java  Application" New topic
Author

Running My First Java Application

Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hello there!

I have downloaded and installed the Java 2 Standard Edition development kit, together with the API documentation.

I have now written a small application from the first chapter of the Head First Java book, to basically print something to the command line.

Once I have saved it as a .java file in my computer, how am i supposed to be able to activate the javac.exe program? Do I use the Run from the start menu? I tried that and there was no success.

I'm obviously missing something big-time here!

Thanks in advance,

Mike


Mike
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Are you using textpad? Download text pad and use that. Simple to compile and run programs.

tools>run java application


Stephen Foy - Microsoft Application Development Consultant
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm assuming that you're using a Windows machine...

Basically, you need to open the Windows Command Prompt. I don't have a Windows machine, but I belive this can be found under Start > Programs > Accessories.

In the Command Prompt, use "cd" to change the directory to where you saved the .java file. For example...

cd c:\myJavaFiles

Then to compile, type (with the .java extension)...

javac MyFileName.java

And after it compiles successfully, run the program by typing (with no extension)...

java MyFileName

For more details, see this Hello World example from The Java Tutorial.
[ January 14, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Marc's instructions assume that you have set the PATH variable. If you haven't done this yet, then check out step 5 in the Installation Instructions. These instructions are for Java 1.4.2, so the exact path to use in the PATH variable will be different if you are using Java 1.5. I hope you will be able to make the necessary adjustments. If not, please come back to tell us what problems you have encountered.

Layne


Java API Documentation
The Java Tutorial
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hi

Thanks ever so much for your replies.

I'm getting rather more success now (Had to blow the cobwebs out of my brain and remember how to play around with DOS).

I have adjusted the PATH environmental variable (I'm running Windows XP), and so I am now able to run the javac program to create the classes as I wanted to.

I am still having difficulty actually running the application, though. The path of the java folder is c:\program files\java. In the java folder are two more folders, jdk1.5.0_06 and jre1.5.0_06 . If I type SET CLASSPATH= into the command line then things happen - but this isn't very satisfactory! What should I set the CLASSPATH variable to to ensure that I can always complile the .java program from any directory on my computer (so that the compiled .class file sits in the same directory)?

Thanks again in anticipation.


Mike
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
At this point, you should NOT use the CLASSPATH variable at all. It will just cause more headaches than it is worth. You should only worry about the PATH variable at this point. In your case, it looks like PATH should contain "c:\program files\java\jdk1.5.0_06\bin". Double check that this is the folder that contains the javac.exe and java.exe programs. If not, you should find where these two programs are installed and put that directory in the PATH variable.

Now when you want to compile a program, you should open a command prompt window and change to the directory where you have saved your .java files. Then you run the following commands:

Of course, you should replace "MyProgram" with the actual name of the class you created. Also notice that when you use the javac compiler, you need to specify a file name which includes the ".java" extension. However, when you run the program with "java MyProgram" you use just the class name without any file name extension. Also, the class name is case sensitive, even if your operating system is not.

I hope this helps you run your program. Please let us know how it works out.

Layne
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hi

Thanks Layne - and everyone else who has been generous with their time on this one. Much appreciated.

Things are actually working - and I am making satisfactory progress.

One more question... Is it necessary to name the .java file the same name as the class that is defined within it?

For example, if, in the java code, I write

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Is it necessary to name the .java file the same name as the class that is defined within it?

In Java it is ;-)
[ January 15, 2006: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Thanks Renee

Mike
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Hudek:
... Is it necessary to name the .java file the same name as the class that is defined within it? ...

Only if that class (or interface) is public.

A .java source file can contain at most one public class or interface definition. (It does not need to contain any.) If a class or interface is declared public, then the source file must share the name of that class or interface.
[ January 15, 2006: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running My First Java Application
 
Similar Threads
What are the benefits of using JavaFx over Swing ?
How to use JSTL in NetBeans
getResource error when starting application in a jar file
My SCJP Adventure
How can I distribute my Java desktop application?