简体   繁体   中英

How to set a temporary Java classpath in Ubuntu

I am writing a Java program and I need to set a temporary classpath that includes my package. The package is on my Ubuntu desktop, which I am importing as /home/gaurav/Desktop. Do you have any idea how to set the Java CLASSPATH temporarily?

You set the Java classpath on Ubuntu the same way as you do on any Linux / UNIX platform (or indeed on Windows ... modulo some minor syntactic differences). There are two ways:

$ java -cp <classpath> some.ClassName arg1 arg2 ... 

or

$ export CLASSPATH=<classpath>
$ java some.ClassName arg1 arg2 ... 

where <classpath> is a sequence of pathnames with ':' separators.

For more details refer to the manual entry for the 'java' command; eg here and here .


If you don't understand export CLASSPATH=... read the Ubuntu manual entry for bash , paying attention to what it says about setting variables, environment variables, and the export built-in shell command. (Hint: $ man bash .)

This is temporary. To make it permanent, add the line to the relevant shell init script; see man bash for details.


how do i get details for my path which path i have set

The classpath is a list of pathnames of directories and JAR files that you want the JVM to search in order to find the classes it needs to run your application. You'll need to figure that out yourself ... or (re-)read the documentation of whatever it is you are trying to run.

If you want to run a java program from the desktop, you have three choices.

The easy choice is to write a small shell script and put that on the desktop. The smallest example might be:

#!/bin/sh
java -cp YOUR_CLASSPATH YOUR_CLASS_NAME "$*"

Next comes the use of 'jarjar' or 'shade' to make one big jar containing all your dependencies, and then run that with java -jar. (As a sub-option, if it's really just for you, you can make a jar with a META-INF/MANIFEST.MF containing a class path with absolute pathnames.)

The more complex choice is to learn to use JNLP to build a launchable item.

IIRC您可以使用环境变量或Java的命令行选项来控制类路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM