[英]Providing path relative to main class for classpaths when running from command line
I am trying to execute a compiled version of my program attachmentUploader.jar
from the command line. 我正在尝试从命令行执行我的程序attachmentUploader.jar
的编译版本。 This has a number of dependencies on various Java libraries, which I am planning to bundle up with the program and specify as part of the classpath. 这对各种Java库有很多依赖关系,我计划将其与程序捆绑在一起并指定为类路径的一部分。 This package will be moved between environments, and will be executed using the Java runtime for that environment. 该软件包将在环境之间移动,并将使用该环境的Java运行时执行。 Using absolute paths for my environment, the command looks like this: 在我的环境中使用绝对路径,该命令如下所示:
"C:\Program Files\Java\jdk1.6.0_37bin\java" -cp "C:/Users/My Documents/jars/attachmentLoader.jar";"C:\Dev Tools\jxl.jar";"C:\Dev Tools\org.apache.commons.codec";"C:\More Dev Tools\java-json.jar" com.custom.test.postClient
I would like to amend all the classpath jar paths to be relative to where my main class is saved, but I read that relative paths in this situation would be relative to where the Java is executed from. 我想将所有类路径jar路径修改为相对于保存我的主类的位置,但是我读到这种情况下的相对路径将相对于Java的执行位置。 In my case this could be anywhere, as it will depend on where this has been installed. 就我而言,它可能在任何地方,这取决于它的安装位置。 Is there a straightforward way of getting round this? 有解决这个问题的简单方法吗?
Thanks all. 谢谢大家
There are actually several ways: 实际上有几种方法:
java -jar path\\to\\jarfile.jar
执行该程序的命令将是java -jar path\\to\\jarfile.jar
I recommend you use a standard build tool such as gradle , which has an application plugin generating all those script files for you, for all platforms, and bundles the whole application into a zip or tar.gz file. 我建议您使用标准的构建工具(例如gradle) ,该工具具有一个应用程序插件 ,可为您在所有平台上生成所有这些脚本文件,并将整个应用程序捆绑为zip或tar.gz文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.