123

How can I compile a .java file?

What programs will I need? If I need the Java JDK I will also need help installing that. I'm very new to Ubuntu, so any program that I need to install I will need a tutorial on how to install them.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
user65407
  • 1,365

6 Answers6

139

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

Raja G
  • 102,391
  • 106
  • 255
  • 328
32

OpenJDK works best for me. It's simple and I have never faced any problem with it. Just follow these simple steps:

  1. From Terminal install open jdk

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

  3. Now to compile use this command from the terminal

    javac filename.java
    

    If everything works well then a new "filename.class" file should be created.

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

842Mono
  • 9,790
  • 28
  • 90
  • 153
Mrinal
  • 771
  • 7
  • 4
18

If for example your file is my_file.java:

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

You want to do:

javac my_file.java

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.

muru
  • 197,895
  • 55
  • 485
  • 740
Akavall
  • 637
  • 2
  • 10
  • 27
  • 1
    I would say your original version was better, since # is the comment character in shells. – muru Nov 03 '14 at 01:58
8

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

javac filename.java

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.

Seth
  • 58,122
4

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.

Fabby
  • 34,259
vicd
  • 501
  • 2
    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 03 '12 at 03:56
  • 7 is only present in 12.04. 6 makes the answer more generic – Thorbjørn Ravn Andersen Jun 03 '12 at 06:39
0

Why is my output so weird when I try to compile a demo example with $ java my_program.java?

I found this program to compile at: [https://developers.google.com/optimization/introduction/java][1]

my_program.java:2: error: cannot find symbol
import com.google.ortools.Loader;
                         ^
  symbol:   class Loader
  location: package com.google.ortools
my_program.java:3: error: cannot find symbol
import com.google.ortools.linearsolver.MPConstraint;
                                      ^
  symbol:   class MPConstraint
  location: package com.google.ortools.linearsolver
my_program.java:4: error: cannot find symbol
import com.google.ortools.linearsolver.MPObjective;
                                      ^
  symbol:   class MPObjective
  location: package com.google.ortools.linearsolver
my_program.java:5: error: cannot find symbol
import com.google.ortools.linearsolver.MPSolver;
                                      ^
  symbol:   class MPSolver
  location: package com.google.ortools.linearsolver
my_program.java:6: error: cannot find symbol
import com.google.ortools.linearsolver.MPVariable;
                                      ^
  symbol:   class MPVariable
  location: package com.google.ortools.linearsolver
my_program.java:11: error: cannot find symbol
    Loader.loadNativeLibraries();
    ^
  symbol:   variable Loader
  location: class BasicExample
my_program.java:13: error: cannot find symbol
    MPSolver solver = MPSolver.createSolver("GLOP");
    ^
  symbol:   class MPSolver
  location: class BasicExample
my_program.java:13: error: cannot find symbol
    MPSolver solver = MPSolver.createSolver("GLOP");
                      ^
  symbol:   variable MPSolver
  location: class BasicExample
my_program.java:16: error: cannot find symbol
    MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
    ^
  symbol:   class MPVariable
  location: class BasicExample
my_program.java:17: error: cannot find symbol
    MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
    ^
  symbol:   class MPVariable
  location: class BasicExample
my_program.java:22: error: cannot find symbol
    MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
    ^
  symbol:   class MPConstraint
  location: class BasicExample
my_program.java:29: error: cannot find symbol
    MPObjective objective = solver.objective();
    ^
  symbol:   class MPObjective
  location: class BasicExample
12 errors
error: compilation failed
Rogonow
  • 91