简体   繁体   English

在 Eclipse 中启动程序与在终端中启动它有何不同?

[英]How is launching a program in eclipse different from launching it in a terminal?

I have a bit of code which works fine when launched from a terminal, but not when launched from within eclipse.我有一些代码在从终端启动时可以正常工作,但在 eclipse 中启动时却没有。 Clearly, I can just use the terminal, but for debugging purposes it would be nice to sort the issue out.显然,我只能使用终端,但出于调试目的,最好能解决这个问题。 Further, it would be nice to understand the issues at play.此外,了解正在发生的问题会很好。 Can anyone explain point me to an explanation of how launching within eclipse differs from launching in a terminal.任何人都可以向我解释在 eclipse 中启动与在终端中启动有何不同。 Code:代码:

static public Process getNewLiEProcess(boolean redirect){
    ProcessBuilder proc = new ProcessBuilder("lie");
    proc.redirectErrorStream(redirect);
    try {
    return proc.start();
    // TODO: figure out why this consistently fails when run in Eclipse console, 
    // even though it seems to work when run in terminal.
    } catch (IOException e) {


    e.printStackTrace();
    return null;
    }
}

Error message when launched in Eclipse:在 Eclipse 中启动时的错误消息:

java.io.IOException: Cannot run program "lie": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at edu.siu.math.egut.io.LiE.getNewLiEProcess(LiE.java:224)
    at edu.siu.math.egut.io.LiE.<init>(LiE.java:198)
    at edu.siu.math.egut.main.CharacterAction.main(CharacterAction.java:106)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:184)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
    ... 3 more
Exception in thread "main" java.lang.NullPointerException
    at edu.siu.math.egut.io.LiE.<init>(LiE.java:202)
    at edu.siu.math.egut.main.CharacterAction.main(CharacterAction.java:106)

When launched using terminal, same code successfully instantiates a "lie" process (the program "lie" is in usr/bin on my system).使用终端启动时,相同的代码成功实例化了一个“谎言”进程(程序“谎言”在我系统的 usr/bin 中)。

The difference can come from:差异可能来自:

  • the java binary used to execute the program用于执行程序的java二进制文件
  • environment variables, especially CLASSPATH , JAVA_HOME , PATH , LD_LIBRARY_PATH (to name just a few)环境变量,尤其是CLASSPATHJAVA_HOMEPATHLD_LIBRARY_PATH (仅举几例)
  • the various flags passed to java传递给java的各种标志
  • working directory工作目录

It's important that you fully understand at all times the requirements of your program in terms of the above factors.就上述因素而言,您必须始终完全了解您的计划要求,这一点很重要。 Once you know all that, it's a matter of verifying that the requirements are correctly setup, whether you execute from the command line and from Eclipse.一旦您了解了所有这些,就需要验证是否正确设置了需求,无论您是从命令行还是从 Eclipse 执行。

If you can make things work on the command line, then the hard part is done.如果您可以在命令行上进行操作,那么困难的部分就完成了。 On the command line it's very easy to verify your environment:在命令行上很容易验证您的环境:

  • java -version to verify the java binary in use java -version验证正在使用的java二进制文件
  • env | sort | less env | sort | less to verify the environment variables in use env | sort | less验证正在使用的环境变量
  • the flags passed to java are right there in the command you execute传递给java的标志就在您执行的命令中
  • pwd to verify the working directory pwd验证工作目录

Usually it's harder to figure things out the other way around, when things work from Eclipse but not from the command line.通常情况下,当事情在 Eclipse 中工作而不是从命令行中工作时,很难以相反的方式解决问题。 Verifying the same details requires a good understanding of Eclipse, and where things are in the menus and project and launcher settings.验证相同的细节需要很好地理解 Eclipse,以及菜单、项目和启动器设置中的内容。

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

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