Cannot execute jar file even though PATH and CLASSPATH are set

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

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

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

However, when I try to start the jar using java -jar weka.jar, I get the error "Could not access jarfile weka.jar".

Any ideas what is going on? I am on Ubuntu Linux. I looked around and it seems that I am not doing anything that is clearly 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 one directory and include this directory in my path (instead of including each jar separately). How can i do this?

Thanks in advance.

EDIT 1 -> Here is 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 the PATH variable to point to the directory '/ home / andy / research / software / weka /' and still β€œcannot access the jarfile error”

+6
source share
5 answers

1) -jar java option disables the use of CLASSPATH. Please see CLASSPATH Setup from Weka

2) Class path entries can contain a wildcard name of the base name, which is considered equivalent to specifying a list of all files in a directory with the extension .jar or .JAR. For example, writing the path to the foo / file indicates all JAR files in a directory named foo. The classpath element, consisting simply of *, expands to a list of all jar files in the current directory. See Setting Class Path | Class Path Template Overview

+9
source

You are not doing as

  java -jar weka.jar 

Instead, you specify the name of one of the INSIDE classes.

simple example without setting the classpath:

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

simple example using set classpath:

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

More complex 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
source

When the jar is in the classpath, it means the library is available for jvm. If you want to run the jar file (execute it main), you still need to specify a valid file path.

+1
source

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

+1
source

Assuming you are in the same folder as weka.jar , you need ./ before the name jar

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

Source: https://habr.com/ru/post/900647/


All Articles