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.
How to compile and run .java files in ubuntu? - Stack Overflow
How to run Java application on startup of Ubuntu Linux - Stack Overflow
Running Java Program from Command Line Linux - Stack Overflow
How to run a java class on ubuntu? - Stack Overflow
Videos
Open the terminal and run:
sudo apt-get install openjdk-7-jdk
and then compile your Java program as before with: javac abc.java. Then run it with:
java abc ## The name of the class to be called is abc NOT abc.class
You can also substitute openjdk-6-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 15.10 and newer, you can also substitute openjdk-8-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 17.10 you can also substitute openjdk-9-jdk. In Ubuntu 17.10 and later you can also substitute openjdk-11-jdk.
In Java 9 and letter, Java has a built-in shell that can run blocks of Java code directly from the terminal without compiling the Java code first, jshell, defined in JEP 222. To start jshell from the terminal type jshell.
$ jshell | Welcome to JShell -- Version 11.0.7 | For an introduction type: /help intro jshell>
To exit from jshell type /exit.
If you prefer to install Oracle JDK, a step by step instruction on installing Oracle JDK 8 is explained in this article : Install Latest Oracle JDK in Ubuntu
Step 1: Download the latest JDK(jdk-Xuxx-linux-xXX.tar.gz) from this official link.
Step 2: Open the terminal (Ctrl + Alt + T) and enter the following command.
sudo mkdir /usr/lib/jvm
Step 3: Enter the following command to change the directory.
cd /usr/lib/jvm
Step 4: Extract the jdk-Xuxx-linux-xXX.tar.gz file in that directory using this command.
sudo tar -xvzf ~/Downloads/jdk-8u45-linux-x64.tar.gz
Step 5: Enter the following command to open the environment variables file.
sudo gedit /etc/environment
Step 6: In the opened file, add the following bin folders to the existing PATH variable.
/usr/lib/jvm/jdk1.8.0_45/bin
/usr/lib/jvm/jdk1.8.0_45/db/bin
/usr/lib/jvm/jdk1.8.0_45/jre/bin
The PATH variables have to be separated by semicolon. Notice that the installed JDK version is 1.8 update 45. Depending on your JDK version, the paths can be different. Add the following environment variables at the end of the file.
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"
The environment file before the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
The environment file after the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_45/bin:/usr/lib/jvm/jdk1.8.0_45/db/bin:/usr/lib/jvm/jdk1.8.0_45/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"
Ideally you should create a service wrapper for your java application and then make this service run on startup example here.
Use
sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu
So two things you'll need to do:
First create a small shell script to start your java program from a terminal. As you have packaged as a jar have a look at this, specifically the JAR Files as Applications section.
This may be sufficient: (although you'll want to use the full path to Java)
#!/bin/bash
java -jar path_to_jar_file
You should be able to run your script and successfully start your program.
Once you've got it starting from a script you can use standard linux tools to start the script. Either putting it in /etc/rc.local, or as you're using Ubuntu, use update-rc.d to start it on boot. See here for a very simple example of using update-rc.d
Hope this helps,
Will
If your Main class is in a package called FileManagement, then try:
java -cp . FileManagement.Main
in the parent folder of the FileManagement folder.
If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:
java -cp . Main
More info about the CLASSPATH and how the JRE find classes:
- How Classes are Found
- Setting the class path (Solaris/Linux)
- http://en.wikipedia.org/wiki/Classpath_(Java)
Guys let's understand the syntax of it.
If class file is present in the Current Dir.
java -cp . fileName
If class file is present within the Dir. Go to the Parent Dir and enter below cmd.
java -cp . dir1.dir2.dir3.fileName
If there is a dependency on external jars then,
java -cp .:./jarName1:./jarName2 fileName
Hope this helps.