compile .java through command prompt

22,262

Solution 1

The JRE has the "java" program for running programs already compiled. The "javac" program is only in the JDK. Download and install the JDK. If BTW it still gives you the same error, then you need to add the javac directory to your PATH environment variable.

Solution 2

Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. Java byte-code is a platform independent version of machine code; the target machine is the Java VM rather than the underlying architecture. To compile a Java source code file add.java, you would do the following:

 javac  add.java

If there are no errors in your source file, the Java compiler will produce one or more .class files (one .class file for each class defined in the add.java source file). For example, the results of a successful compile of Foo.java will produce a byte-code version of the class in a file named Foo.class.

Every public class that you write must be in a separate .java file where the first part of the file name is identical to the class name. The .java file additionally can contain code for protected and private classes.

Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code:

 java <class with main method to run> [<command line args>, ...] 

For example, to run the main method from the Foo class:

 java Foo

Any command line arguments (arguments to add's main method) follow the class name:

 java add 10 20

Such Error may occur due to two reasons:

  1. You have not installed java jdk on your system.
  2. You have not set the environment variables.classpath ,path.

Setting Path and classPath:

  • Windows XP

    • Select Start, select Control Panel. double click System, and select the Advanced tab.
    • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it.
    • Click Edit. If the PATH environment variable does not exist, click New.
    • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
  • Windows Vista:

    • From the desktop, right click the My Computer icon.
    • Choose Properties from the context menu.
    • Click the Advanced tab (Advanced system settings link in Vista).
    • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it.
    • Click Edit. If the PATH environment variable does not exist, click New.
    • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

If you have not set the classpath and path you can access javac giving full path:

such as C:\Java\jdk1.7.0\bin\javac MyClass.java

To check the path and classpath, type these commands in a command window:

echo $PATH
echo $CLASSPATH

If you get a blank command line in response to either of these, then that particular variable has no value (it has not yet been set).

setting path and classpath through cmd:

set path=c:\j2sdk1.4.1_01\bin(Give the path of bin)

set classpath=;(or the directory where you want your class files)
Share:
22,262
chipp
Author by

chipp

Updated on May 14, 2020

Comments

  • chipp
    chipp about 4 years

    I'm new in programming, I'm learning Java right now. I tried to use the javac command, but the environment says that javac is an unknown command.

    How to use "javac" to compile .java files from the command prompt? I'm using eclipse 1.2.2.20100216-1730, JRE 1.6 and JRE6

  • Dave Richardson
    Dave Richardson about 12 years
    This answer looks to have been lifted almost entirely from cs.swarthmore.edu/~newhall/unixhelp/debuggingtips_Java.html - I think it would have been nice to either reference the original article or at least attribute where it was copied from, even if you wrote it yourself.
  • Abhishek Singh
    Abhishek Singh about 12 years
    Yes some lines , may be , but it is was short so just explained it