To compile the file, open your terminal and type
javac filename.java
To run the generated class file, use
java filename
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
Answer from Raja G on askubuntu.comTo compile the file, open your terminal and type
javac filename.java
To run the generated class file, use
java filename
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
OpenJDK works best for me. It's simple and I have never faced any problem with it. Just follow these simple steps:
From Terminal install open jdk
sudo apt-get install openjdk-7-jdkWrite a java program and save the file as filename.java
Now to compile use this command from the terminal
javac filename.javaIf everything works well then a new "filename.class" file should be created.
To run your program that you've just compiled type the command below in terminal:
java filename
NOTE
You can use any text editor (like gedit) ,
replace the filename with watever name you want
you need to be on same directory as the "present working directory" (got by running pwd) while running the command from terminal.
Can somebody teach me how to compile Java Programs from the command line?
How do I compile a program via terminal on macbook?
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
Videos
You're almost there buddy!
Just be sure you have the Java Development Kit (JDK) installed in your system. The JDK provides you the command javac -which you need to compile your .java program files.
The javac command is not that friendly as you think it is. You have to let it know where to find the java file you want to compile.
For example you saved Main.java inside C:\Projects\Java you must compile it this way javac C:\Projects\Java\Main.java.
Then you can run your program this way too java C:\Projects\Java\Main
As mentioned here... You can find a more detailed explanation to the official tutorial.
You need to install java JDK which you can find here
Then you need to right-click on my computer->properties->advanced system settings->Environment variables -> Click on path -> New
Then add the path to the bin folder inside the install folder for JDK.
Then in cmd you can compile with
javac *.java
inside the directory of your code
I have no idea how to compile from the command line. My previous teacher neglected to teach us this, and I only know how to use Eclipse. Alright, so my issue was more about getting the PATH variable and file placement, not so much the actual commands
SOLVED
Hello All. I'm new to programming and java. I'm trying to compile a program via terminal on a macbook and I keep running into errors. I was wondering what I need to change to fix this issue. First I changed the directory of my terminal to the folder that has the .java program in it. I ran "ls" to verify that my .java program was in that directory and it was. However, when I run javac Planet.java I get 60+ errors. This is just a small trial code nothing elaborate. Further info: I have had no issues compiling this code when using an online compiler called CodingGround found here: https://www.tutorialspoint.com/compile_java_online.php I just seem to have issues when using the terminal.
Here is my code (I removed spaces in case I had invisible characters that were causing me issues):
class Planet{
void revolve(){
System.out.println("Revolve");
}
public static void main(String[] args){
Planet earth = new Planet();
earth.revolve();
}
}
I have also tried making it a "public" class by changing the first line to public class Planet{ and still run into the same issues.
Picture of Errors Here:
https://postimg.cc/qNGHhnPk
https://postimg.cc/QK7RXWgZ
PS: Solution: was solved by thisisjustascreename: The text editor is the problem. Macbook's default TextEdit.app dose not automatically save in plain text. You have open the file and go to Format up at the top and then click on Make Plain Text and after saving it and making sure it is still just a .java file and run it via terminal with the javac command it will work just fine.
Source: javaindos.
Let's say your file is in C:\mywork\
Run Command Prompt
CopyC:\> cd \myworkThis makes C:\mywork the current directory.
CopyC:\mywork> dirThis displays the directory contents. You should see filenamehere.java among the files.
CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\binThis tells the system where to find JDK programs.
CopyC:\mywork> javac filenamehere.javaThis runs javac.exe, the compiler. You should see nothing but the next system prompt...
CopyC:\mywork> dirjavac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
CopyC:\mywork> java filenamehereThis runs the Java interpreter. You should then see your program output.
If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!
To complete the answer :
The Java File
Copy
TheJavaFile.javaCompile the Java File to a *.class file
Copy
javac TheJavaFile.java- This will create a
TheJavaFile.classfile
- This will create a
Execution of the Java File
Copy
java TheJavaFileCreation of an executable
*.jarfileYou've got two options here -
With an external manifest file :
Create the manifest file say - MANIFEST.mf
The MANIFEST file is nothing but an explicit entry of the Main Class
jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class
Executable by Entry Point:
jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
To run the Jar File
Copy
java -jar TheJavaFile.jar