从我们的Java程序执行Java程序 [英] Execute a Java program from our Java program

查看:100
本文介绍了从我们的Java程序执行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过

Runtime.getRuntime().exec("_____")

但它会抛出 IOException ,如下所示:

but it throws a IOException as below:

java.io.IOException: CreateProcess: c:/ error=5
  at java.lang.Win32Process.create(Native Method)
  at java.lang.Win32Process.<init>(Win32Process.java:63)
  at java.lang.Runtime.execInternal(Native Method

我不知道在指定路径或其他方面是否有问题。任何人都可以帮我解释代码。

I don't know whether I have the problem with specifying the path or something else. Can anyone please help me with the code.

推荐答案

您正在尝试执行C:/。您需要执行以下操作:

You're trying to execute "C:/". You'll want to execute something like:

javaw.exe d:\\somejavaprogram\\ program.jar

注意路径分隔符。

我假设这是一个特别的项目,而不是大的。但是,从代码运行外部程序的最佳实践:

I'm assuming this is for an ad-hoc project, rather than something large. However, for best practice running external programs from code:


  • 不要硬编码可执行文件位置,除非你确定它永远不会改变

  • 看看像%windir%这样的目录使用System.getenv

  • 不要假设像javaw.exe这样的程序在搜索路径中:首先检查它们,或者允许用户指定位置my program.jar。

  • Don't hardcode the executable location, unless you're certain it will never change
  • Look up directories like %windir% using System.getenv
  • Don't assume programs like javaw.exe are in the search path: check them first, or allow the user to specify a location
  • Make sure you're taking spaces into account: "cmd /c start " + myProg will not work if myProg is "my program.jar".

这篇关于从我们的Java程序执行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆