Java重生过程
我正在制作一个类似编辑器的程序。如果用户在主窗口中选择“文件”->“打开”,我想以所选文件名作为参数启动编辑器进程的新副本。但是,为此我需要知道使用什么命令来启动第一个进程:
java -jar myapp.jar blabalsomearguments // --- need this information
> Open File (fileUrl)
> exec("java -jar myapp.jar blabalsomearguments fileUrl");
我不是在寻找进程内解决方案,我已经实现了该解决方案。我希望获得单独流程带来的好处。
I'm making an editor-like program. If the user chooses File->Open in the main window I want to start a new copy of the editor process with the chosen filename as an argument. However, for that I need to know what command was used to start the first process:
java -jar myapp.jar blabalsomearguments // --- need this information
> Open File (fileUrl)
> exec("java -jar myapp.jar blabalsomearguments fileUrl");
I'm not looking for an in-process solution, I've already implemented that. I'd like to have the benefits that seperate processes bring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您正在启动 Java -> Java,可以使用现有的类路径在命令行上设置类路径。这种事情在开发环境中也非常有效。
更新:
对于可执行的 jar 文件,您将有一个类路径,它只是 jar 文件本身的相对路径。如果您需要命令行参数,则必须从 main 中保存它们,并在启动时重新应用它们。
您可以通过将以下程序打包到 jar 中来看到这一点。我实际上不确定如果可执行 jar 文件中有 jar 会发生什么。它们可能出现在类路径中。
对于
java -jar ..\tst.jar X
,您会得到如下输出:Since you are launching Java -> Java, you can use the existing classpath to set the classpath on the command line. This type of thing works really nice in the dev environment too.
Update:
For executable jar files, you will have a classpath which is simply the relative path to the jar file itself. If you want the command line arguments, you will have to save them from main, and re-apply them when launching.
You can see this by packing the following program into a jar. I'm not actually sure what happens if you have jars inside the executable jar file. They probably show up in the classpath.
For
java -jar ..\tst.jar X
, you get output like:如果所有其他方法都失败,请尝试编写批处理/shell 脚本来启动您的应用程序。在 Windows 中,您可以将 %CmdCmdLine% 传递给 Java 以获取整个命令行。
请参阅http://www.robvanderwoude.com/parameters.php
If all else fails, try writing a batch/shell script to launch your app. In windows you can pass %CmdCmdLine% to Java to get the entire command line.
See http://www.robvanderwoude.com/parameters.php
据我所知,没有可移植的方式来获取此信息。我在 gcj 运行时找到了一个属性,但我怀疑这是否会覆盖大部分用户。
我认为公认的做法是“尝试并祈祷”:
希望它在路径上,(路径可用,以便可以检查)
如果没有,则检查 JAVA_HOME 是否已定义,并使用它来查找 java.util.JAVA_HOME 。
如果没有检查您收到的错误报告的所有操作系统上最有可能的位置。
好吧,这很混乱......可能最好检查 JAVA_HOME 和路径,并要求用户在失败时明确配置 JVL。
As far as I know is there no portable way to get this info. I found a property in the gcj runtime but I doubt this will cover a large percentage of the users.
I think the accepted practice is "Try and Pray" :
Hope it is on the path, (the path IS available, so that can be checked)
if not, check if JAVA_HOME is defined, and use that to find java.
if not check in the most likely places on all OS's you have received bug reports for.
Well, it is messy... porbably best to check for JAVA_HOME and the path and ask the user to configure a JVL explicitely if that fails.