简体   繁体   中英

How do I configure Oracle Java JDK 7 (not Open JDK) correctly on Ubuntu 11.10 and run a Java Program with referenced JAR files?

I may have multiple questions here, but ultimately I'm trying to compile and run a Java program that references 3rd party JAR files (on Ubuntu Linux) so I thought it was appropriate to keep it to one question.

I'm trying to get Java JDK 7 properly installed and configured to run a simple program on Ubuntu Linux. I'd like to get instructions from an expert that can help me, at the very least, to run a simple Java program from the command line (perhaps a "Hello World" application?). I've only compiled and run Java programs on Windows in the past, so this is new to me. Last night, I read that you have to be careful of not installing the Open JDK, and to install the Oracle JDK. It appears that Ubuntu 11.10 installs Java Open JDK for you when the OS is installed, among others.

I'm running 32-bit Ubuntu, not 64-bit. So I downloaded this Linux JDK file:
jdk-7u4-linux-i586.tar.gz

I unpacked the tarball into /usr/java and then deleted the *.gz file to save disk space. Some tutorials say to install into /usr/java and some say to install into /usr/lib/jvm, so I just picked one and rolled with it. If you have a suggestion on the appropriate install directory, please let me know what you suggest.

I then checked what Java version was installed (below) with the "java -version" command. It did not find the Oracle JDK.

(1) Why didn't the command list the Oracle JDK package I just installed?

mac@UBUNTU:/usr/lib$ java -version
The program 'java' can be found in the following packages:
 * gcj-4.4-jre-headless
 * gcj-4.6-jre-headless
 * openjdk-6-jre-headless
 * gcj-4.5-jre-headless
 * openjdk-7-jre-headless
Try: sudo apt-get install <selected package>
mac@UBUNTU:/usr/lib$ 

(2) Can you help me with the steps necessary to configure this Linux machine to compile a Java program (finish what I just started)?

(3) Can you help me with the steps to compile a *.java file to create the *.class file?

(4) Can you help me run the Java program--the one we just compiled?

(5) Suppose I want to include multiple *.jar files (3rd party assemblies) in my Java program. How does Java know on Linux where to find these files if they are not in the directory running the Java program referencing them? (I'm only familiar with a CLASSPATH in Windows)

  1. Ubuntu is based on Debian and its apt-get utility. apt-get will list only known packages. The one you installed is just a folder lying on your system and apt-get does not even know about it.
  2. It looks like java is not on your path. So either you create a symbolic link to the java binary in one of the folders of your path (type env to see what's in your path variable), or you add the JDK bin directory to your path (see command export ). As a last resort, you can type the absolute path to the java binary, but that's just painful.
  3. To compile, you simply use javac (Java compiler). For example javac Test.java . Make sure that javac is also on your path (or use the absolute path to the java compiler)
  4. To run it, type java -cp . Test java -cp . Test (don't forget to type the fully qualified name of your class if you put it inside a package, which is recommended)
  5. When you compile or when you run, use the argument -cp followed by the list of jars you want to use (separated by a ':' on Linux and by a ';' on Windows). You may use wildcards in your classpath (like lib/*.jar) but you may also have to escape the wildcard to avoid shell expansion (like this lib/\\*.jar ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM