为什么我在这两种情况下会遇到不同的行为?我错过了什么吗?
命令:
ObservableLinkedList<String> list = new ObservableLinkedList<>();
list.addListener((Change<? extends String> c) -> {
while (c.next()) {
if (c.wasAdded()) {
System.out.println("Added from "+c.getFrom()+" to "+c.getTo()+" "+c.getAddedSubList());
}
if (c.wasRemoved()) {
System.out.println("Removed from "+c.getFrom() + " to "+c.getTo()+" "+c.getRemoved());
}
if (c.wasUpdated()) {
System.out.println("Updated");
}
if (c.wasPermutated()) {
System.out.println("Permutated");
}
}
});
list.addAll("Two", "Three", "Four");
list.offerFirst("One");
list.offer("Five");
System.out.println(list.pollFirst());
System.out.println(list.pollLast());
堆栈追踪:
new ProcessBuilder().directory(
Paths.get(System.getProperty("user.dir")).toFile())
.command("/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb").start();
失败案例中的环境:
Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Cannot run program "/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb" (in directory "/Users/myusername/myproject"): error=2, No such file or directory
at com.comcast.tvx.app.xreserver.Main.exec(Main.java:47)
at com.comcast.tvx.app.xreserver.Main.main(Main.java:16)
Caused by: java.io.IOException: Cannot run program "/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb" (in directory "/Users/myusername/myproject"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at com.comcast.tvx.app.xreserver.Main.exec(Main.java:44)
... 1 more
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:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 2 more
更多信息
当我使用Runtime.exec()执行此操作时,环境看起来完全相同,但我没有收到错误:
命令:
{PATH=/usr/bin:/bin:/usr/sbin:/sbin
JAVA_STARTED_ON_FIRST_THREAD_1074=1
SHELL=/bin/zsh
SECURITYSESSIONID=186a4
USER=myusername
JAVA_MAIN_CLASS_25188=com.comcast.tvx.app.xreserver.Main
APP_ICON_1074=../Resources/Eclipse.icns
COMMAND_MODE=unix2003
TMPDIR=/var/folders/t_/dlj2wfdj0bx2xl6mnnqmxyhj99pf4b/T/
SSH_AUTH_SOCK=/tmp/launch-Bhd1It/Listeners
DISPLAY=/tmp/launch-PuSx66/org.macosforge.xquartz:0
__CF_USER_TEXT_ENCODING=0x529B388B:0:0
Apple_PubSub_Socket_Render=/tmp/launch-hB7zpQ/Render
__CHECKFIX1436934=1
LOGNAME=myusername
HOME=/Users/myusername}
成功案例中的环境:
Runtime.getRuntime().exec("/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb")
答案 0 :(得分:8)
尝试更改此行:
.command("/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb").start();
to(向右滚动以查看差异):
.command("/usr/bin/java -Djava.library.path=/Users/myusername/myproject/lib/DynamoDBLocal_lib/ -jar /Users/myusername/myproject/lib/DynamoDBLocal.jar -sharedDb".split("\\s+")).start();
说明:command()
的输入应该是字符串的数组(或列表),每个字符串都是另一个参数(或&#34;标记&#34;)。它不应该包含空格!
来自docs:
命令,表示外部程序文件的字符串列表 被调用及其参数,如果有的话。哪个字符串列表代表 有效的操作系统命令取决于系统。例如,它 每个概念参数都是此列表中的元素, 但是有些操作系统需要程序 tokenize命令行字符串本身 - 在这样的系统上是Java 实现可能需要命令包含两个元素。