How to get the java compiler for ubuntu?

14,241

Chances are your colleagues are using Java 6. In that case, you have to install openjdk-6-jdk.

Recall that single .java source files are compiled using:

javac hello.java

.class files are executed like:

java Hello

This expects a Hello.class file in the current directory. If you're working in a different directory, you need to set the -classpath (or -cp) option:

java -cp /path/to/classes Hello
Share:
14,241

Related videos on Youtube

Jordan
Author by

Jordan

Updated on September 18, 2022

Comments

  • Jordan
    Jordan over 1 year

    I am in a Java class in my college right now, and we have to be able to compile java programs via the terminal. I would like to keep using my Ubuntu installation for it though. whenever I try to compile something on Ubuntu, nobody can read the program. Says something about it not being able to read the program. I have installed the "openjdk-7-jdk" program, which might be the issue. Any help is appreciated, thanks! On a side note, I would like to mention that I need to compile my programs on OS X Lion or Windows 7 in order to run them correctly...

    Here is a picture of what I have downloaded and what I could download instead...

    enter image description here

    • Michael K
      Michael K over 12 years
      before invoking java, you need to compile your program using javac: javac ToMyDearestRachel (you might need to add .java extension if you have one) - I guess Rachel will be the second happiest girl in the world (just after mine) once it works :)
    • Jordan
      Jordan over 12 years
      The problem is I have already used javac. It still doesn't work on Ubuntu.
    • Michael K
      Michael K over 12 years
      ah re read your posting - yes you should use java 6 as java 7 is not yet that common ;-) Or thell the others that java 7 is now available. The different versions of java are installed in the folder /usr/lib/jvm/whatever/bin/javac - if you intend to use another installed java compiler, just invoke the command using the complete path
    • Jordan
      Jordan over 12 years
      Should I just uninstall java 7 if it isn't all that common anyways?
    • Michael K
      Michael K over 12 years
      you can keep it installed, but you might want to setup the version 6 as default by redirecting the symlinks javac and java
    • Jordan
      Jordan over 12 years
      Ah well I just removed jdk7 and installed jdk6 and it works flawlessly right now! thanks you for the suggestions! :)
  • Jordan
    Jordan over 12 years
    I am in the correct directory, it just won't run for some reason.
  • Lekensteyn
    Lekensteyn over 12 years
    Try using openjdk-6-jdk as I suggested. It's also very unusual to share .class files in class. Usually, the .java source files must be submitted to the teacher (and collegues)
  • Jordan
    Jordan over 12 years
    Yeah we have to submit the .java files, but I can't even test on my machine if the programs are actually working. I'll try java 6.
  • Lekensteyn
    Lekensteyn over 12 years
    Using the sudo update-alternatives --config java command, you can choose your default java program. For javac, replace java by javac
  • Jordan
    Jordan over 12 years
    Java 6 works flawlessly! I guess it was just too updated haha. Thanks for the help!