6

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'

user721975
  • 1,257
  • 3
  • 13
  • 14
  • Try using `$HOME` instead of `~`? – Emil Sit Nov 02 '11 at 22:21
  • Well actually I have used the full path: /home/andy/software/weka/weka.jar. Sorry about that. – user721975 Nov 02 '11 at 22:25
  • 1
    -jar means that the classpath will be ignored – micfra Nov 02 '11 at 22:27
  • you can only put all jar files in a single directory and include that directory in classpath only in java 6 and later. And you need to use wildcard notation. – Atilla Ozgur Nov 03 '11 at 13:50
  • I added an answer for your problem. But what you are doing is. Trying to access a class named weka jar. What java is doing : "trying to find a package named weka, inside that package class named jar, inside that class main method" – Atilla Ozgur Nov 04 '11 at 13:28

5 Answers5

9

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

micfra
  • 2,780
  • 1
  • 23
  • 34
  • When I run it without -jar, it gives me a "Exception in thread "main" java.lang.NoClassDefFoundError: weka/jar" exception. – user721975 Nov 02 '11 at 23:01
  • Please edit your question adding your CLASSPATH and your command line. Have you compared it with the link to Weka wiki posted above? – micfra Nov 03 '11 at 08:05
  • Yes I did. I was able to invoke weka using 'java -cp $CP -Xmx500m weka.gui.explorer.Explorer' but I really want to be able to execute the jar file directly (java weka.jar) since I want a general way of executing jar files by including them in the path. I have also update the question as you had suggested. Thanks. – user721975 Nov 03 '11 at 17:32
  • Please try those both things: 1) java weka.gui.explorer.Explorer and 2) java -jar weka.jar weka.gui.explorer.Explorer. Seems that weka developers did not add the main class to the manifest file so java does not know what to do. – micfra Nov 04 '11 at 08:00
4

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
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
1

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.

mqsoh
  • 3,180
  • 2
  • 24
  • 26
  • 1
    Sorry, I don't understand. Then what is the point of adding it to the classpath if every time I need to provide the full path anyway. – user721975 Nov 02 '11 at 22:27
1

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.

javagirl
  • 1,635
  • 6
  • 27
  • 43
  • Well that's the thing. I don't want to be in the folder all the time and want to be able to run the jar file still. PATH variable is in fact pointing to the directory '/home/andy/research/software/weka/'. – user721975 Nov 03 '11 at 19:11
  • @user721975 ok then why you cannot just run 'java -jar /my/path/to/jar' ? I think it's anyway not the best idea - to put the location of your one particular jar to the the system 'path' variable – javagirl Nov 07 '11 at 10:16
0

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
scottyab
  • 23,621
  • 16
  • 94
  • 105