Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Java Program

Status
Not open for further replies.

sujitopics

Programmer
Oct 9, 2001
69
KR
Dear Friends
I am new to Java
A Strange Doubt.
Can I run my java program like this...

java HelloWorld.class

My HelloWorld program is like this

class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World ");
}
};

After Compiling ...Can i Run the program as like

java HelloWorld.class ?

If anyone having idea...Please participate in this discussion.

Thanks in advance

Yours
Suji
 
Dear borisha
Thankyou VeryMuch.
Please tell me the procedure of setting of classpath and path to do like that.
Thanks in advance
Yours
Suji
 
Setting the classpath and path will depend on the OS you're using. For Win 95/98/ME, set path = %path; <directory of your compiler\bin Ex: c:\jdk1.3\bin>; <directory of your compiler\lib Ex: c:\jdk1.3\lib> So,
set path = %path%; c:\jdk1.3\bin; c:\jdk1.3\lib
or something similiar to that
For the classpath it's merely just adding the directory you are storing your class source code in (the .java files).
set classpath = c:\my_java_programs (for example)

To run your java program, you would type java HelloWorld rather than java HelloWorld.class since the runtime environment is already looking for .class files.

Hope that helps.

JavaDude32
 
Actually, for small programs, it's much easier to run the program like this:

java -cp . HelloWorld

This means, set the classpath (-cp) to the current directory.

Note that using the .class ending WON'T work. You'll understand why when you learn about packages.

In general, setting up the classpath for every noddy program you write will lead to an unnecessarily long classpath with many redundant entries.

Generally, it's much better to create a script file which sets up the classpath and runs the program (for programs you run regularly):

HelloWorld.bat:
@echo off
set classpath=c:\my_java_program
java c:\my_prog\HelloWorld

This is very useful if a program uses many java libraries from different people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top