• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Running My First Java Application

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using textpad? Download text pad and use that. Simple to compile and run programs.

tools>run java application
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mike Hudek
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Mike Hudek
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Renee

Mike
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 coderanch.

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 coderanch, then the source file must share the name of that class or interface.
[ January 15, 2006: Message edited by: marc weber ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic