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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET developer with a newbie Java compiler query

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I'm learning Java, coming over from the Microsoft .NET world. As such, I've got Microsoft's language compilers set as various environment variables on my WinXP development box. Thus, I do all my command-line compiling from within the default directory for the compiler: C:\j2sdk1.4.0_03\bin
(I'm stating this all to make sure I'm getting it, too...sorry, gurus. :) )

Basically, I can create other directories and successfully compile a Java program from within BIN\ (I assume it compiles because the .CLASS file(s) are created) by using the following compile command:
javac -classpath . ..\samples\HelloWorld.java

...but I'm having a touch time executing the programs. I've tried numerous commands at the prompt, but I can't seem to get it to work.

Any suggestions?

Thanks!
 
Hi jasonsalas:

This FAQ may help you:

faq269-3042

As the CLASSPATH enviroment variable explained in the FAQ, the PATH variable could be used so you can start java and javac from any directory, not just the c:\jdk1.4\bin, by the following line:

set CLASSPATH=%CLASSPATH%;c:\jdk1.4\bin

or editing the enviroment variables as explained in the FAQ for XP.

You will have to change your jdk version someday, so it is better to have a directory outside the jdk hierarchy to place your codes.

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Thanks guys. I don't think I can set the CLASSPATH environment variable, because I've got the Microsoft .NET Framework SDK previously installed, and it's already set some variables, such as PATH.

So, I've been creating .JAVA files in the same directory as the Java compiler, and then executing them in this directory. This isn't too safe, so I'd rather do it from other directories below \BIN.

That's why I was asking what command-line syntax could be used to compile/execute a Java program, say for \BIN\SAMPLES.

Any ideas?

Thanks!
 
Thanks. What if I have PATH and CLASSPATH variables already defined by the .NET Framework?
 
Hi jasonsalas:

No matter if .NET has set the variables. There is only one CLASSPATH or PATH environment variable for the system that each new DOS console inherits. So you can change its contents appending new information to the variable as podollb2003 said.

When you type:

set PATH=%PATH%;c:\anotherdir

you are extracting the variable contents with the % modifier (%PATH%), so that command will append the information to the variable.

ANYVAR=1;2;3

set ANYVAR=%ANYVAR%;4 -> ANYVAR==1;2;3;4

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
OK...I'll try it. Thanks again! LIke I said, .NET is what I get paid to do, and I'm experimenting with Java to broaden my hozirons, so I don't want to do something that will catastrophically alter my normal work. Your feedback was great!
 
As an additional note, when you duplicate any component such as Java on the PATH or CLASSPATH environment variables, the first found will be used. For instace, if you want to use your C:\j2sdk1.4.0_03\bin\ version, but the .NET SDK appears before it in the PATH, the C:\j2sdk1.4.0_03\bin\ will never be used. If you want to supply the PATH or CLASSPATH explicitly whenever your compile/run Java, you can do so with the -cp, -classpath options for CLASSPATH and the -D option for PATH. For example, start your program like this:

java -cpC:\j2sdk1.4.0_03\jre\lib\rt.jar -DPATH=C:\j2sdk1.4.0_03\bin\ myprogram

You can also create a system environment varialbe that points to your Java home (C:\j2sdk1.4.0_03) called JAVA_HOME and use it in the above call to java. In fact, this setting is generally required for J2EE.

java -cp%JAVA_HOME%\jre\lib\rt.jar -DPATH=%JAVA_HOME%\bin\ myprogram
 
Thanks sigrney,

Is there a way to explicitly state the paths for both the compiler, and then the JRE? For example, when working in c:\j2sdk1.4.0_03\bin, trying to execute a file at c:\j2sdk1.4.0_03\bin\examples

using relative paths:
java -cp . MyProgram/examples
java -cp . ../../MyProgram/examples

...or absolute paths:
java -cp c:\testdir\examples\MyProgram.java

...I think this is the syntax I'm after. You can do this with the .NET language compilers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top