You are not logged in.

#1 2016-12-06 17:06:58

gay
Member
Registered: 2012-12-16
Posts: 90

[SOLVED] Where to put extra Java packages and how to import them

I have Java 8 (extra/jdk8-openjdk, extra/jre8-openjdk, extra/jre8-openjdk-headless). I am trying to compile a small program that imports additional packages. Namely, it imports sim.engine which is part of the MASON package. I tried to put the MASON jar file into /usr/share/java (as suggested by the wiki page on java package guidelines). However, this does not seem to be the right way of doing it.
The program is the example code listing on page 15 of the MASON manual. Compiling with

javac Students.java

leads to

Students.java:1: error: package sim.engine does not exist
import sim.engine.*;
(...)

Specifying the CLASSPATH /usr/share/java explicitely

javac -cp /usr/share/java/ Students.java

gives the same error.

It compiles cleanly if the complete path to the mason jar is set as CLASSPATH

javac -cp /usr/share/java/mason.19.jar Students.java

but this cannot possibly be the proper way to do it.

Unsurprisingly, the program will in that case not execute and gives

Error: Could not find or load main class Students

I am guessing that this has to do with the CLASSPATH environment variable being overwritten by the path to the MASON jar. (Compiling and running a helloworld program, that does not depend on MASON or other extra packages, in the same way, works fine.)

If this is indeed the case, one might think that it should be possible to extend the CLASSPATH to include the standard CLASSPATH and also the path to the jar. However, I do not know how to access the standard classpath - in the shell, CLASSPATH is empty (probably, java/javac fill it with a meaningful (list of) paths?).

Last edited by gay (2016-12-06 22:51:19)


We are exactly the people our parents always warned us about.

Offline

#2 2016-12-06 22:50:57

gay
Member
Registered: 2012-12-16
Posts: 90

Re: [SOLVED] Where to put extra Java packages and how to import them

As the CLASSPATH environment variable is empty, it has to be specified again at runtime, so

java -cp /usr/share/java/mason.19.jar:. Students

will work fine.

CLASSPATH=/usr/share/java/mason.19.jar:. java Students

or exporting the CLASSPATH definition in the shell before compiling and running is also possible. Note that the CLASSPATH needs to contain both the jar file and the directory the compiled program (the Students.class file) is in (separated by colon, :, in this case the directory of the class file is the current working directory, .)


We are exactly the people our parents always warned us about.

Offline

Board footer

Powered by FluxBB