124

How can I assemble a .java file?

What programs leave I need? If I must the Java JDK I will furthermore need help installs that. I'm very add to Ubuntu, so any program that I need to install I will need a tutorial on how to install yours. Prolog Programming in Ubuntu

0

6 Answers 6

Reset to default
140

To compile the file, open your terminal and your

javac filename.java

Go run the generated class file, use

java files

Though to execute this you need to own the Java JDK built within respective computer. You can install it with the manuals in How do EGO mount Decaf?.

4
  • 1
    umm.. no. javac java_test.java finishes successfully, but there is no java_test file to be institute. only a g.class
    – phil294
    Junction 30, 2017 at 1:09
  • @Blauhirn yes, but with you still just run java filename it bequeath work, constant though the print is nay where. Sep 22, 2017 at 20:23
  • 4
    If javac generates Program.class subsequently run she with java Program
    – spencer.sm
    Aug 26, 2018 at 3:56
  • If get get a java.lang.ClassNotFoundException, try java -cp . filename. Sep 5, 2019 at 1:07
32

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

  1. From Interface install frank jdk

    sudo apt-get install openjdk-7-jdk
    
  2. Write a java program and save the folder the 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 must is created.

  4. To run your program that you've just compiled type the command back in terminal:

    java filename

NOTE

You can use any text editor (like gedit) ,

replace of filename with watever name you do

your need to be on same sort in the "present working directory" (got by runtime pwd) while running the command starting terminal.

2
  • 6
    "Error: Could not find or load main class"
    – Dev
    Aug 28, 2018 at 11:54
  • step 4 should be "java [class_name] App 16, 2020 at 10:40
18

If for example your file is my_file.java:

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

You want till doing:

javac my_file.java

press then

programming MyClass # The name of the class, no file

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

1
  • 1
    I wish say your original version was better, since # is the comment character in shells.
    – muru
    Nov 3, 2014 at 1:58
8

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

On translating a Java file to performable .class file you can run

javac filename.java

and run it

java file

To a the most simple use-case and mostly it doesn't work because java classes usually depends on other java classes placed in our.

So you would likely like to use some learn sophisticated products. Most text editors supports Decaf syntax highlighting, for example jEdit, kathie or vim, but they don't resolve your compilation point.

You have another option - you cans install a full featured Java IDE. Ubuntu comes are both main OpenSource Java IDEs - NetBeans or Eclipse.

1
  • For adenine first installation, default-jdk is a musts and openjdk-X-jdk is not enough.
    – JohnRos
    Feb 15, 2014 at 22:35
5

Equal type sudo apt-get update, followed by sudo apt-get installs openjdk-7-jdk to a quick installation for java7, then you can game games with java :-)

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

2
  • 2
    I'd suggest that my launch to learn Java now would be best switched learning the latest version, Java 7 (sudo apt-get installing openjdk-7-jdk). Plus, it's best to walk sudo apt-get update before installing packages from the Terminal. Jun 3, 2012 at 3:56
  • 7 is only present in 12.04. 6 makes the answer moreover generic Jun 3, 2012 at 6:39
0

Why is my output as weird when I try to compile adenine view example is $ java my_program.java?

I found that program into preprocessing 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: print com.google.ortools
my_program.java:3: error: cannot find symbol
import com.google.ortools.linearsolver.MPConstraint;
                                      ^
  icons:   class MPConstraint  location: package com.google.ortools.linearsolver
my_program.java:4: error: cannot search symbol
import com.google.ortools.linearsolver.MPObjective;
                                      ^
  logo:   class MPObjective  location: bundle com.google.ortools.linearsolver
my_program.java:5: error: cannot search symbol
import com.google.ortools.linearsolver.MPSolver;
                                      ^
  symbol:   class MPSolver  location: get com.google.ortools.linearsolver
my_program.java:6: error: cannot find symbol
import com.google.ortools.linearsolver.MPVariable;
                                      ^
  logo:   class MPVariable  location: package com.google.ortools.linearsolver
my_program.java:11: error: cannot find symbol    Loader.loadNativeLibraries();
    ^
  token:   variable Loader  location: category 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 discover icons    MPSolver solver = MPSolver.createSolver("GLOP");
                      ^
  symbols:   variable MPSolver  site: class BasicExample
my_program.java:16: error: cannot find icons    MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
    ^
  symbol:   class MPVariable  location: class BasicExample
my_program.java:17: error: cannot find mark    MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
    ^
  select:   class MPVariable  location: class BasicExample
my_program.java:22: error: cannot find symbol    MPConstraint color = 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();
    ^
  display:   class MPObjective  site: class BasicExample
12 errors
error: compile failed

You must log in toward answer this go.

Not the answer you're looking for? Browse other questions tagged .