LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2005, 02:48 PM   #1
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
Java libraries in Linux


I'm running Ubuntu Breezy (5.10), and I just installed the libcommons-cli-java package, so that I could take advantage of its argument parsing features.

It installed the file /usr/share/java/commons-cli-1.0.jar and the symlink (to the aforementioned file) /usr/share/java/commons-cli.jar.

Unzipping the jar gets us:
Code:
Archive:  commons-cli.jar
   creating: META-INF/
  inflating: META-INF/MANIFEST.MF
   creating: org/
   creating: org/apache/
   creating: org/apache/commons/
   creating: org/apache/commons/cli/
  inflating: org/apache/commons/cli/AlreadySelectedException.class
  inflating: org/apache/commons/cli/BasicParser.class
  inflating: org/apache/commons/cli/CommandLine.class
  inflating: org/apache/commons/cli/CommandLineParser.class
  inflating: org/apache/commons/cli/GnuParser.class
  inflating: org/apache/commons/cli/HelpFormatter$1.class
  inflating: org/apache/commons/cli/HelpFormatter$StringBufferComparator.class
  inflating: org/apache/commons/cli/HelpFormatter.class
  inflating: org/apache/commons/cli/MissingArgumentException.class
  inflating: org/apache/commons/cli/MissingOptionException.class
  inflating: org/apache/commons/cli/Option.class
  inflating: org/apache/commons/cli/OptionBuilder.class
  inflating: org/apache/commons/cli/OptionGroup.class
  inflating: org/apache/commons/cli/Options.class
  inflating: org/apache/commons/cli/ParseException.class
  inflating: org/apache/commons/cli/Parser.class
  inflating: org/apache/commons/cli/PatternOptionBuilder.class
  inflating: org/apache/commons/cli/PosixParser.class
  inflating: org/apache/commons/cli/TypeHandler.class
  inflating: org/apache/commons/cli/UnrecognizedOptionException.class
So my question is, how do I import this? I've tried
Code:
import org.apache.commons.cli.*
which is how the examples I've seen do it. But the only way I've been able to compile with this, is by unzipping into my working directory.

If someone could tell me where I need to place/rename the jar, any environment variables to set, or anything at all that could help, I would appreciate it.

Thanks,
Chris
 
Old 12-03-2005, 06:22 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Java relies on something called a "class path".

Java has a default classpath; you can augment/override this via an environment variable
(EXAMPLE:
export CLASSPATH=".:/usr/share/java/commons-cli-1.0.jar")

or with the "java -cp" command line argument
(EXAMPLE:
java -cp .:/usr/share/java/commons-cli-1.0.jar myApp)

It turns out that CLASSPATH (and whether or not your app will actually run) has little to do with the "import" statement. The latter is a "compile time thing"; the former is an "execution time thing".

It also turns out that you can include either a directory (as you would expect) or a .Jar file in your CLASSPATH. This means that you do *not* necessarily have to extract the contents of a .Jar file in order to use it.

Here are a couple of links which might help:

http://www.freshsources.com/Sep99.html
<= Somewhat dated, but still good!

http://mindprod.com/jgloss/classpath.html
<= For Windows, but still applicable

'Hope that helps .. PSM
 
Old 12-04-2005, 12:20 AM   #3
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Rep: Reputation: 30
Quote:
Java has a default classpath; you can augment/override this via an environment variable
(EXAMPLE:
export CLASSPATH=".:/usr/share/java/commons-cli-1.0.jar")
To make sure that the .jar file is in the class path add the above line to ~/.bashrc or ~/.bash_profile
It is a good idea to add $CLASSPATH to your class path so that previously set libraries are included as well.

Last edited by Basel; 12-04-2005 at 12:25 AM.
 
Old 12-04-2005, 01:28 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Actually, Chrax, I would encourage you to play with *BOTH* the $CLASSPATH environment variable *AND* the "-classpath" command line option.

One can make a strong case that the "$CLASSPATH" environment variable is obsolete. It certainly doesn't scale well beyond "hello world" test programs and specific apps (which launch their own individual JVM from inside of a script).

In any case, please post back and let us know if you solved the problem!

Your .. PSM
 
Old 12-05-2005, 11:03 PM   #5
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
Yes, I got it. Thanks.

I've never played with Java libraries before. C and Perl are much more straightforward as far as includes go, but then, they don't really have anything like the equivalent of .jar's.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java + OpenGL libraries hylke Programming 10 11-02-2004 09:52 AM
missing java swing libraries? a1ghagh05t Programming 5 01-26-2004 06:04 AM
gcc3 support libraries for java plugin on Moz Firebird lel800 Linux - Software 0 10-16-2003 03:44 PM
Program not finding java VM libraries gtanghookup Linux - Software 5 09-04-2003 01:20 AM
Java Libraries not functioning downinthemine Linux - Newbie 1 07-31-2003 09:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration