简体   繁体   English

Java 未被识别为 eclipse 的内部或外部命令,但在 cmd 中运行良好

[英]Java is not recognized as an internal or external command for eclipse but works well from cmd

I am running a sw developped under eclipse (in java) from the cmd.我正在从 cmd 运行在 eclipse(在 java 中)下开发的 sw。 It works well.它运作良好。 Once I want to run it from一旦我想从

I am adding a runnble java sw (B.jar) to another java program (A).我正在向另一个 java 程序 (A) 添加一个可运行的 java sw (B.jar)。 The "call" of the B.jar works well from the cmd and does the job correctly. B.jar 的“调用”在 cmd 上运行良好,并且工作正常。 Once called from the program (A), it gives me the error of the "unrocognized command".一旦从程序(A)中调用,它就会给我“无法识别的命令”的错误。 I execute the program (A) from eclipse.我从 eclipse 执行程序 (A)。 Have we to configure some additional information in Eclipse?我们是否需要在 Eclipse 中配置一些附加信息? Or maybe have I to execute directly from the distribution of the software (A)或者也许让我直接从软件的分发中执行 (A)

ScreenShot for the execution from cmd:从 cmd 执行的屏幕截图: 在此处输入图像描述

Screenshot from the ececution from eclipse: eclipse 的执行截图: 在此处输入图像描述

2020-06-24 12:57:26,172 [Thread-7] INFO - 'java' n'est pas reconnu en tant que commande interne 2020-06-24 12:57:26,173 [Thread-7] INFO - ou externe, un programme ex‚cutable ou un fichier de commandes. 2020-06-24 12:57:26,172 [Thread-7] INFO - 'java' n'est pas reconnu en tant que commande interne 2020-06-24 12:57:26,173 [Thread-7] INFO - ou externe, un program ex‚cutable ou un fichier de commandes。 2020-06-24 12:57:36,141 [Thread-6] INFO - Appuyez sur une touche pour continuer... 2020-06-24 12:57:36,141 [Thread-6] INFO - Appuyez sur une touche pour continuer...

The script from (A) to call (B):从 (A) 到调用 (B) 的脚本:

private final InputStream inputStream;

class Flux implements Runnable {

    private final InputStream inputStream;

    Flux(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    private BufferedReader getBufferedReader(InputStream instream) {
        return new BufferedReader(new InputStreamReader(instream));

    }

    public void run() {
        BufferedReader br = getBufferedReader(inputStream);
        String line = "";
        try {
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

String command =  B_SW_AbsolutePath + " "+ usersCommand;
try {
            Runtime rt = Runtime.getRuntime();
            this.proc = rt.exec(command, {""}, outputDir);
            Trace.info("execution "+command+" "+outputDir.toString());
            
            Flux outputStream = new Flux(
                    this.proc.getInputStream());
            Flux errorStream = new Flux(
                    this.proc.getErrorStream());
            new Thread(outputStream).start();
            new Thread(errorStream).start();
            this.proc.waitFor(10,TimeUnit.SECONDS);
        } catch (IOException e) {
            Trace.erreur("External software can not be run " + e.getMessage());
        }
        
        finally{
            
            this.process.destroy(); 
        }
    }

Thank you so much for your help非常感谢你的帮助

By using the Runtime.exec(String cmd,String[] envp,File dir) overload with {""} for envp you have provided the child process with no environment variables and particularly no PATH envvar, equivalent to an empty one.通过使用带有{""} for envp 的Runtime.exec(String cmd,String[] envp,File dir)重载,您为子进程提供了没有环境变量,特别是没有 PATH envvar,相当于一个空的。 Since your batch file apparently runs a simple program name java instead of a complete pathname, it needs to be looked up in the PATH variable and since the PATH variable is empty that lookup fails.由于您的批处理文件显然运行一个简单的程序名称java而不是完整的路径名,因此需要在 PATH 变量中查找它,并且由于 PATH 变量为空,因此查找失败。 If you don't want to or can't change the batch file to specify complete paths for everything (and otherwise not depend on any envvars) -- and if necessary similarly change everything it runs -- the best solution is to pass null for the second (envp) argument to exec so it uses the default which is the parent Java's own envvars.如果您不想或不能更改批处理文件以指定所有内容的完整路径(否则不依赖于任何环境变量)——并且如果需要类似地更改它运行的所有内容——最好的解决方案是null传递给exec的第二个 (envp) 参数,因此它使用默认值,即父 Java 自己的 envvars。 The harder method is to construct a list of the correct envvars (with values) manually, and pass that.更难的方法是手动构建正确 envvars(带有值)的列表,然后传递它。

Also, instead of concatenating "script"+" "+"argument" which Runtime.exec must re-split, you can use the String[] overload with new String[]{"script","command"} .此外,您可以将String[]重载与new String[]{"script","command"}一起使用,而不是连接Runtime.exec必须重新拆分的"script"+" "+"argument" Depending on the compiler you might not need the new String[] ;根据编译器,您可能不需要new String[] I haven't kept track of all the type-deduction changes because I usually prefer to be explicit.我没有跟踪所有类型推断的更改,因为我通常更喜欢明确。 But since you want the combination for your tracing, maybe not.但是,既然您想要跟踪的组合,也许不是。

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

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