简体   繁体   English

在Java中使用命令行时是否将项目文件夹设置为标准?

[英]Setting project folder as standard when using command line in Java?

I am using a provided python script in a project which I call using 我在使用的项目中使用了提供的python脚本

Process p=Runtime.getRuntime().exec("cmd /c cd C:\\Users\\Kevski\\Desktop\\EKI Projekt\\Route Planning\\tools\\ && python draw.py coordinates.route");

It creates an image from the coordinates.route file which I later use again in Java. 它从coordinates.route文件创建一个图像,稍后我在Java中再次使用它。 It also works fine - on my computer. 它也可以正常工作-在我的计算机上。 My question is can I automatically let the path above be adjusted to the project folder so that it works on any machine? 我的问题是我可以自动将上面的路径调整为项目文件夹,以便在任何计算机上都可以使用吗?

Thanks very much in advance! 首先十分感谢!

This is what Properties are for. 这就是Properties的用途。 You could define the path in a property file and read it and then use it in the command. 您可以在属性文件中定义路径并读取它,然后在命令中使用它。 As a simple example, you cold pass the path from java command line (if running from command line) or in web/app server configuration file for -D parameter. 举一个简单的例子,您可以通过java命令行(如果从命令行运行)或-D参数的web / app服务器配置文件中的路径进行冷传递。 When in command line, send path as a parameter as : 在命令行中,将path作为参数发送为:

java -Dpath.to.command="/usr/local/" YourClassName

In the class, the property can be read as : 在类中,该属性可以读取为:

String path = System.getProperty("path.to.command");

If path is not null, construct the complete command using variable path and proceed. 如果path不为null,则使用变量path构造完整的命令并继续。 Thus you have externalized the path, and then can be passed from command line in any environment. 因此,您已经外部化了路径,然后可以在任何环境中从命令行进行传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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