How to include a jar when executing a Java file by command line

5

Whenever I run the program I need to call a specific library, which in this case is /home/usuario/Programas/weka-x-x-x/weka.jar , but every time I run the program, I have to include the following java -cp /home/usuario/Programas/weka-x-x-x/weka.jar command for the libraries to run. >

I would like to know how to make this library include by default in the CLASSPATH, I found this command in Stack Overflow:

export CLASSPATH="dir1;dir2;path/to/jar/containing/AnotherClass;... but it did not work.

    
asked by anonymous 16.03.2015 / 18:56

1 answer

4

Define libraries without changing Java

Setting% global% is not recommended. It can cause unwanted side effects in other programs.

To avoid repetition, create a shell script file containing the commands you need, use classpath to set execute permissions for the file and use the new "executable" instead of calling Java directly when you need it.

Using the variable chmod

If you still want to set% global%, the correct is using the environment variable CLASSPATH . However, replace the semicolon with a colon by separating directories, as follows:

export CLASSPATH="dir1:dir2:path/to/jar/containing/AnotherClass:...

The semicolon is used in Windows environments while the colon is used in Unix / Linux environments.

In addition, if you want the environment variable to be present in all Linux sessions, even after rebooting the system, you will need to add the classpath command to some boot script.

Changing the Java installation

As noted in this SOen response , you can place the jar in the CLASSPATH folder of the Java installation: / p>

JAVA_HOME/jre/lib/ext

Again, I do not advise doing this. Even very common libraries can sometimes generate conflict. For example, some older project may depend on an older version of the same jar used by a newer application.

    
16.03.2015 / 20:34