Compiling from command line with javac
I am trying to compile my first JAVA file using command line (yes, I know the existence of IDEs).
My App.java sits inC:\Documents and Settings\
My jre is in C:\Program Files\Java\jre1.5.0_10\
But I get following error.
C:\Documents and Settings>javac App.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
What has gone wrong?
Thanks.
The JRE is the runtime environment. You only use it to run Java programs.You need the JDK to compile programs. The JDK comes with the JRE, so you may or may not have it already.
Thank you Paul.How can I determine if I have the JDK and how can I compile my java program from the command line.Thanks.
ancino_man,
if you have the JDK (15 hours is a looooong time... I think I heard once that Sun will send you a copy on CD upon request) then make sure C:\Program Files\Java\jdk1.5*\ is in your path.
if you don't know how to do that confidently then google "change windows path"... I really really love google.
keith.
R U on winblows? which version?
The JDK is not installed by default on most windows systems. If you didn't get it yourself, then you probably don't have it.
To compile a java program, you need to have the JDK. If you don't have it, download it. Just click on the "downloads" link at the top of the page and look for J2SE downloads. Download the Windows JDK (aka SDK).
> The JDK comes with the JRE, so you may or may not have it already.
Saying it that way is a bit confusing: You can either download the JRE
or the JDK. If you just download the JRE you can only run compiled
programs; if you download the JDK, the JRE comes with it; as a matter
of fact you get *two* JREs: one with which you run programs and one
used by the JDK itself.
kind regards,
Jos
Thanks Paul.Will download the JDK.
I have managed to compile App.java and App.class is present (verified with Windows Explorer. But when I run the command "java App.class", an error message was displayed (please see the following).
********** from the command prompt
C:\Program Files\Java\jdk1.6.0\bin>javac C:\App.java
C:\Program Files\Java\jdk1.6.0\bin>java App.class
Exception in thread "main" java.lang.NoClassDefFoundError: App/class
************************************
However, the same App.java run without any problems when loaded onto Eclipse SDK.
Can anyone enlighten?
Thanks.
The Java JVM doesn't look in the current working directory for classes by
default. It checks the 'classpath' which is a list of directories and .jar
files for .class files. You can specify this classpath on the command line:
java -classpath . YourApp
Note the dot '.' it represents your current working directory.
kind regards,
Jos
The JVM executable (that is, the program "java") takes a class name, not a file name.So don't run this:java App.classRun this:java App
Thanks!
I am now able to run the bytecode but only when the .class resides in the same directory as the jdk. Before line 4, I physically moved App.class into
C:\Program Files\Java\jdk1.6.0\bin>
(see the following).
******** from command prompt
C:\Program Files\Java\jdk1.6.0>cd bin
C:\Program Files\Java\jdk1.6.0\bin>java c:\App
Exception in thread "main" java.lang.NoClassDefFoundError: c:\App
C:\Program Files\Java\jdk1.6.0\bin>java App
Hello World!
C:\Program Files\Java\jdk1.6.0\bin>
************************************
After "Hello World!" is displayed on the screen, I moved the App.java back into c:\and issued the following command:
C:\Program Files\Java\jdk1.6.0\bin>java -classpath c:\
******* result from command prompt
:
:
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
:
:
*************************************
How should I correct this?
Thanks.
This command do not work either. See post No. 12 for the results.C:\Program Files\Java\jdk1.6.0\bin>java -classpath c:\App
> This command do not work either. See post No. 12 for> the results.> > C:\Program Files\Java\jdk1.6.0\bin>java -classpath> c:\Appinsert 'space' between c:\ and App