Unable to execute jar file despite having PATH and CLASSPATH set

删除回忆录丶 提交于 2019-12-10 04:35:11

问题


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'


回答1:


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




回答2:


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



回答3:


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.




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/7988024/unable-to-execute-jar-file-despite-having-path-and-classpath-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!