user721975
user721975

Reputation: 1267

Unable to execute jar file despite having PATH and CLASSPATH set

My question is regarding including jar files in path. It has 2 parts.

1) I am trying to execute weka.jar jar file located in /home/andy/software/weka/weka.jar

PATH variable points to this jar file (i.e. to /home/andy/software/weka/weka.jar) and so does CLASSPATH.

However when I try to run the jar using java -jar weka.jar, I get an error "Unable to access jarfile weka.jar".

Any ideas what is going on? I am on Ubuntu Linux. I looked around in SO and it seems like I am not doing anything that is obviously wrong (since both PATH and CLASSPATH seem to be set correctly).

2)I would like to be able to put all my jar files in a single directory and include that directory in my path (instead of including every jar individually). How can I do that?

Thanks in advance.

EDIT 1 -> Here's my command line

andy@laptop:~$ export CLASSPATH=$CLASSPATH:/home/andy/research/software/weka/weka.jar
andy@laptop:~$ echo $CLASSPATH
:/home/andy/research/software/weka/weka.jar
andy@laptop:~$ java -jar weka.jar
Unable to access jarfile weka.jar
andy@laptop:~$ java weka.jar
Exception in thread "main" java.lang.NoClassDefFoundError: weka/jar
Caused by: java.lang.ClassNotFoundException: weka.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: weka.jar.  Program will exit.
andy@laptop:~$ 

EDIT 2 -> I changed PATH variable to point to directory '/home/andy/research/software/weka/' and still get 'unable to access jarfile error'

Upvotes: 6

Views: 27587

Answers (5)

scottyab
scottyab

Reputation: 24039

assuming your in the same folder as the weka.jar all you need is ./ before the jar name

java -jar ./weka.jar weka.core.SystemInfo

Upvotes: 0

Atilla Ozgur
Atilla Ozgur

Reputation: 14701

You do not execute as

 java -jar weka.jar

Instead you give name of one of the classes INSIDE jar.

a simple example without setting classpath :

 java -jar /home/andy/research/software/weka/weka.jar weka.core.SystemInfo

a simple example using set classpath:

 export CLASSPATH=$CLASPATH:/home/andy/research/software/weka/weka.jar

 java weka.core.SystemInfo

More complicated example:

export WEKA_HOME=/home/andy/research/software/weka/

export CLASSPATH=$CLASPATH:$WEKA_HOME/weka.jar

export HEAP_OPTION=-Xms4096m -Xmx8192m
export JAVA_COMMAND=java $HEAP_OPTION

$JAVA_COMMAND weka.core.SystemInfo

Upvotes: 4

javagirl
javagirl

Reputation: 1675

Unable to access *.jar means that java cannot find this jar file. You should either run this command (java -jar weka.jar) from folder where your jar is located, or correct your PATH variable. It should point to the FOLDER where the jar is placed, not the jar file directly.

Upvotes: 1

micfra
micfra

Reputation: 2820

1) -jar option of java disables the use of CLASSPATH. Please take a look at Setting the CLASSPATH of Weka

2) Class path entries can contain the basename wildcard character , which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/ specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. Have a look at Setting the class path|Understanding class path wildcards

Upvotes: 8

user8710
user8710

Reputation:

When the jar is in the classpath it means that the library is available to the jvm. When you want to run a jar file (execute it's main), you still need to provide a valid path to the file.

Upvotes: 1

Related Questions