Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Java JDK to compile on ubuntu

Tags:

java

linux

I'm trying to start working with Java, but so far haven't been able to get it on my machine properly. I'd really like to be able to compile from command line. After following the instructions here with no errors I can't compile with javac. Here's what I have so far:

When I enter:

$ java -version

I get:

java version "1.6.0_16"  
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)  
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)

When I run:

$ sudo apt-get install sun-java6-jdk

I get:

~$ sudo apt-get install sun-java6-jdk  
Reading package lists... Done  
Building dependency tree          
Reading state information... Done  
Suggested packages:  
  sun-java6-demo sun-java6-doc sun-java6-source  
The following NEW packages will be installed:  
  sun-java6-jdk  
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.  
Need to get 17.4MB of archives.  
After this operation, 55.7MB of additional disk space will be used.  
WARNING: The following packages cannot be authenticated!  
  sun-java6-jdk  
Install these packages without verification [y/N]? y  
Err http://us.archive.ubuntu.com hardy-updates/multiverse sun-java6-jdk 6-07-3ubuntu2
404 Not Found [IP: 91.189.88.140 80]  
Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/multiverse/s/sun-java6/sun-java6-jdk_6-07-3ubuntu2_i386.deb  404 Not Found [IP: 91.189.88.140 80]  
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

When I run:

$ /media/disk/School/java/hw1$ javac HelloWorldApp,java  </pre>

I get:

The program 'javac' can be found in the following packages:  
 * java-gcj-compat-dev  
 * openjdk-6-jdk  
 * gcj-4.2  
 * kaffe  
 * ecj  
 * jikes-sun  
 * jikes-sablevm  
 * j2sdk1.4  
 * jikes-classpath  
 * jikes-gij  
 * gcj-4.1  
 * sun-java5-jdk  
 * jikes-kaffe  
 * sun-java6-jdk  
Try: sudo apt-get install <selected package>  
bash: javac: command not found

When I try to update (using sudo apt-get update) I get:

E: The method driver /usr/lib/apt/methods/https could not be found.

Has anyone else encountered this problem? Thanks in advance...

like image 801
danwoods Avatar asked Sep 03 '09 04:09

danwoods


People also ask

How do I run a Java JDK?

To run the JDK installer: Start the JDK 13 installer by double-clicking the installer's icon or file name in the download location. Follow the instructions provided by the Installation wizard. After the installation is complete, delete the downloaded file to recover the disk space.

Which JDK is best for Ubuntu?

The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 18.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK. To install this version, first update the package index: sudo apt update.


3 Answers

You can install the JDK on recent versions of Ubuntu by typing this command:

sudo apt-get install sun-java6-jdk

You might find this easier than attempting to set it up manually.

like image 76
Matthew Talbert Avatar answered Oct 13 '22 01:10

Matthew Talbert


Try this:

  1. Download the Java SDK into $HOME/archives (e.g., $HOME/archives/jdk-6u16-linux-x64.bin).
  2. Extract Java into /opt (or another location if you do not want to use root). For example:
cd /opt
chmod 755 $HOME/archives/jdk-6u16-linux-x64.bin
sudo $HOME/archives/jdk-6u16-linux-x64.bin</code>
  1. Create a symbolic link (to ease upgrades):
sudo ln -s jdk1.6.0_16 jdk
  1. Edit $HOME/.bashrc
  2. Append the following lines:
JAVA_HOME=/opt/jdk
PATH=$PATH:$JAVA_HOME/bin
  1. Reload the environment variables:
source $HOME/.bashrc

You should now be able to compile programs.

I prefer this method to installing the managed package because uninstalling (or upgrades) never seems to remove all bits of the SDK flawlessly, and it seems to hinder installing multiple versions of the Java Software Development Kit on the same machine at the same time. I have had issues with apt-get and Java in the past. Also, this method allows me to be absolutely certain which version of Java is in use at any time.

If you are not comfortable using root and /opt, you can use your own account and $HOME/bin/jdk instead. Change the .bashrc file accordingly.

Remove any version of Java you previously had installed. You might need to restart your terminal session.

This works for all versions of Java since at least Java 1.2.

like image 32
Dave Jarvis Avatar answered Oct 13 '22 00:10

Dave Jarvis


Usually you will find java in the PATH and not javac in a standard Ubuntu installation. This is primarily because of the gcj package that gets installed. Symlinks are also created that can be updated using the update-alternatives script.

After an installation of Sun JDK, you are required to update the symlink to java, and this is usually done via a command similar to the one below

sudo update-alternatives --config java

If hardlinks to the location of (Sun) java is not present, you can create it using a command similar to

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_07/jre/bin/java 300

In the case of javac, you can create a symlink, again using update-alternatives using:

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_07/bin/javac 300

This will make javac available on PATH, just like java.

Of course, there is option of updating the PATH variable via a simple export or via changes to the shell configuration file.

like image 42
Vineet Reynolds Avatar answered Oct 13 '22 01:10

Vineet Reynolds