java - 使用 'java filename'命令运行java文件

标签 java eclipse terminal execution

当您不在文件中时,我想使用“java filename”命令运行java文件。

在终端中我们使用:

cd filepath

java filename

但是在 Eclipse 中,您无法使用“cd”更改目录,所以尽管我无法更改目录,但如何运行该文件

我正在使用此方法来使用 Java 运行命令

try {
           String line;
           Process p = Runtime.getRuntime().exec( "cd /Users/apple/Documents/Documents/workspace/UserTesting/src" );
           Process p2 = Runtime.getRuntime().exec( "java NewFile" );

           BufferedReader in = new BufferedReader(
                   new InputStreamReader(p2.getInputStream()) );
           while ((line = in.readLine()) != null) {
             System.out.println(line);
           }
           in.close();

     }
     catch (Exception e) {
           // ...
         }

这里有几个试验

Apples-MacBook-Pro:~ apple$ cd /Users/apple/Documents/Documents/workspace/UserTesting/src Apples-MacBook-Pro:src apple$ java NewFile 5 90 35 45 150 3
Reichweite---- nach blase art
3 5 35 45 90 150

Apples-MacBook-Pro:src apple$ java /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile Exception in thread "main" java.lang.NoClassDefFoundError: /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile Caused by: java.lang.ClassNotFoundException: .Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Apples-MacBook-Pro:src apple$ java /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile.java Exception in thread "main" java.lang.NoClassDefFoundError: /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile/java Caused by: java.lang.ClassNotFoundException: .Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile.java at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Apples-MacBook-Pro:src apple$ Blockquote

最佳答案

这不会起作用。

首先,cd 是一个 shell 命令。然后创建另一个进程来运行java命令。

您需要一个 ProcessBuilder 。您可以设置初始目录、环境等:

final File wantedCwd = new File(...);
final ProcessBuilder pb = new ProcessBuilder("java", "thefile");

// Change directory
pb.directory(wantedCwd);

您甚至可以更改命令的 stdin、stdout、stderr 等。

不要使用Runtime.exec()。在许多情况下它不会 (exec())。

关于java - 使用 'java filename'命令运行java文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135467/

相关文章:

node.js - MAC OS X "open APP.app"和 "APP.app/content/MacOs/APP"shell 脚本之间的差异

bash - 修剪文件名以仅保留最后 5 个字符

java - 是否可以使用 Java 在 Google App Engine 上拥有计算属性?

java - 参数化测试解决方案

java - 在 Eclipse 中禁用 "Chosen option is not enabled"消息

java - 关于在 eclipse 中使用库项目

java - 无法理解连接建立代码

java - 递归骑士之旅中变量的正确声明(Java作业)

eclipse - 为整个项目设置环境变量

linux - Bash:使用文件名作为带通配符的命令