简体   繁体   中英

How to specify the path to jar in java source code?

In my program, I am importing some functions defined in a class file in jar. And whenever I compile or run the code, I always need to specify the path to the file with -cp ./:/path/to/jar .

Is there any way that I can specify the jar path in my source code? So that I don't need to type -cp ./:/path/to/jar every time I compile or run the code?

The classpath is used by the VM to find your classes. You cannot specify it in the source code. If you are looking for a shortcut then you can write a wrapper script that calls your program with the classpath that you desire.

You could change your packaging approach instead. If you created a 'fat jar' containing your code and the other classes from the other jar(s) you are dependent on, then you could run your app with a 'java -jar yourjar.jar'. Maven Shade is one way to do this, see: https://maven.apache.org/plugins/maven-shade-plugin/

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