Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I compile a .java file. What programs will I need and if I need java jdk I need help installing that. I'm very new to ubuntu so any program I need to install I will need a tutorial on how to install them.

share|improve this question
up vote 20 down vote accepted

To compile the file, open your terminal and type


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?.

share|improve this answer

OpenJDK work best for me. It's simple and I have never face any problem. Just follow these simple steps:

  1. From Terminal install the open jdk

    sudo apt-get install openjdk-7-jdk
  2. Write program and save the file as

  3. Now to compile use this command from the terminal


    If everything work well you will notice a filename.class file is also created.

  4. To run your program you compiled use the below code in terminal:

    java filename


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.

share|improve this answer

You need to install a JDK, Java Development Kit. Ubuntu contains a metapackage default-jdk, which depends on currently prefered JDK. Now it is openjdk-6-jdk.

To compile a Java file to runnable .class file you can run


and run it

java file

It is the most simple use-case and mostly it doesn't work because java classes mostly depends on other java classes placed in libraries.

So you would probably like to use some more sophisticated solutions. Most text editors supports Java syntax highlighting, for example jEdit, kate or vim, but they don't solve your compilation issue.

You have another option - you can install a full featured Java IDE. Ubuntu comes with both main OpenSource Java IDEs - NetBeans and Eclipse.

share|improve this answer
For a first installation, default-jdk is a must and openjdk-X-jdk is not enough. – JohnRos Feb 15 '14 at 22:35

If for example your file is

class MyClass
    public static void main(String[] args)
       System.out.println("Hello World");

You want to do:


and then

java MyClass # The name of the class, not file

However, it is a common convention to give classes and files the same name.

share|improve this answer
Thanks a lot it worked – MaheshK Apr 10 '14 at 6:57
I would say your original version was better, since # is the comment character in shells. – muru Nov 3 '14 at 1:58
Then I change it back. Thanks. – Akavall Nov 3 '14 at 1:59

Just type sudo apt-get update, followed by sudo apt-get install openjdk-7-jdk for a quick installation for java7, then you can play games with java :-)

For more detailed official Java documentation, please visit this link.

share|improve this answer
I'd suggest that people starting to learn Java now would be best off learning the latest version, Java 7 (sudo apt-get install openjdk-7-jdk). Also, it's best to run sudo apt-get update before installing packages from the Terminal. – Eliah Kagan Jun 3 '12 at 3:56
7 is only present in 12.04. 6 makes the answer more generic – Thorbjørn Ravn Andersen Jun 3 '12 at 6:39

protected by Community Jun 5 at 18:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.