简体   繁体   English

从命令行运行时为类路径提供相对于主类的路径

[英]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: 实际上有几种方法:

  1. Make your jar file an executable jar file. 使您的jar文件成为可执行的jar文件。 That is done by adding a manifest file to the jar specifying what the main class is, and where the jar dependencies are located, relative to the location of the main jar file. 这是通过将清单文件添加到jar中来完成的, 该清单文件指定主类是什么,以及jar依赖项相对于主jar文件的位置位于何处。 The command to execute the program would then be java -jar path\\to\\jarfile.jar 执行该程序的命令将是java -jar path\\to\\jarfile.jar
  2. Use a wrapper executable or shell script, that uses the location of the script itself to compose the classpath. 使用包装可执行文件或外壳程序脚本,该脚本使用脚本本身的位置来组成类路径。 Such a script would depend on the system (Unix/Windows), but both platforms allow script files to know their own location, and thus to use that location as a basis for the location of jar files. 这样的脚本将取决于系统(Unix / Windows),但是两个平台都允许脚本文件知道它们自己的位置,因此可以将该位置用作jar文件位置的基础。 An advantage of that technique is that it also allows passing system properties, memory options, etc. to the JVM. 该技术的优点是它还允许将系统属性,内存选项等传递给JVM。

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.

相关问题 错误:从Linux命令行运行时找不到或加载主类Main- - Error: Could not find or load main class Main - when running from Linux command line 通过命令行运行时找不到或加载主类 - Could not find or load main class when running through command line 从命令行运行程序时找不到 Class - Class not found when running program from command line 类路径和命令行开关(-cp,-D) - Classpaths and command-line switches (-cp, -D) 从命令行运行 jar 时覆盖 pom.xml 中指定的主类 - Override main class specified in pom.xml while running jar from command line 运行Ant taskdef时有多个类路径? - Multiple classpaths when running Ant taskdef? 从命令行运行主方法时无法访问静态方法 - Cannot access static method when running thru main method from command line 从命令行运行java程序时,在线程“main”java.lang.NoClassDefFoundError'中出现异常 - 'Exception in thread “main” java.lang.NoClassDefFoundError' when running java program from command line Java Jar 文件主类在从命令行运行时运行,而不是从 Windows 批处理文件运行 - Java Jar file main class run when runs from command line, but not from Windows batch file 在Linux上提供多个目录作为类路径(带有路径分隔符)时,无法从命令行运行Java应用程序 - Cannot run java application from command line while providing multiple directories as classpath ( with path separator) on linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM