从 Rhino JS 控制台运行 CD 会产生异常

发布于 2024-12-03 08:21:59 字数 325 浏览 1 评论 0原文

当尝试从 js.jar 控制台内部执行 runCommand("cd", "..") 时,会抛出 IOException。
我相信这是因为在命令提示符中 CD 命令实际上内置于控制台中,而不是单独的 .exe 文件。 runCommand("notepad") 工作正常,并且可以在类路径的常用位置找到该 .exe。

有解决办法吗?

我在想通过java而不是通过命令提示符更改目录可能会解决这个问题,但我不记得如何从java做到这一点,但我计划尝试弄清楚这一点。

总结一下:有没有办法在 Windows 上的 Rhino JS 控制台中运行“cd”?

谢谢, L-

When attempting runCommand("cd", "..") from inside a js.jar console an IOException is thrown.
I believe it's because in the command prompt the CD command is actually built into the console and not a separate .exe file. The runCommand("notepad") works fine, and that .exe can be found on the classpath in the usual location.

Is there a work around for this?

I was thinking that changing the directory through java instead of through the command prompt it might solve this problem, but I don't recall how to do that from java, but I plan on trying to figure that out.

To sum up: Is there a way to run "cd" from within a Rhino JS console on windows?

Thanks,
L-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-12-10 08:21:59

这里需要解决多个问题;这取决于你想做什么。

runCommand 实际上运行可执行程序。 cd 在 Windows 上不是可执行文件;它是命令 shell 中的命令。因此,您需要执行类似这样的操作:

runCommand("cmd","/c","cd <target-directory>")

但是,底层 Java 运行时不允许您实际更改工作目录。请参阅此 StackOverflow 讨论。因此,删除 cd 只是更改了子进程(运行 cd 的进程)的目录,这可能不是您想要的。

Multiple issues to solve here; it depends what you are trying to do.

runCommand actually runs executable programs. cd is not an executable on Windows; it is a command in the command shell. So you would need to execute something more like this:

runCommand("cmd","/c","cd <target-directory>")

However, the underlying Java runtime does not allow you to actually change the working directory anyway. See this StackOverflow discussion. So shelling out cd just changes the directory for the subprocess (the process running cd), which is probably not what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文