简体   繁体   English

Java Shell命令未从当前目录运行

[英]Java Shell Command not Running from Current Directory

In this question it shows that when you run a shell command from Java, it runs from the current directory. 这个问题中,它表明当您从Java运行shell命令时,它从当前目录运行。 When I run the command javac Program.java from my program, it shows the error (from the standard error stream): 当我从我的程序运行命令javac Program.java ,它显示错误(来自标准错误流):

javac: file not found: Program.java
Usage: javac <options> <source files>
use -help for a list of possible options

However, when I run the same exact command from the actual Terminal, it works fine and saves the .class file in the default directory. 但是,当我从实际的终端运行相同的命令时,它工作正常并将.class文件保存在默认目录中。 This is the code: 这是代码:

Runtime rt = Runtime.getRuntime();
String command = "javac Program.java";
Process proc = rt.exec(command);

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new 
     InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
    System.out.println(s);
}
proc.waitFor();

Any ideas why it works when I type it in the actual Terminal, but not when I run it from my program? 当我在实际的终端中键入它时,有什么想法可以解决它,但是当我从我的程序中运行它时却没有? I am running a Max OS X Mountain Lion (10.6) 我正在运行Max OS X Mountain Lion(10.6)

Thanks 谢谢

Your program is probably being run from a different directory. 您的程序可能正在从其他目录运行。 Your IDE (such as Eclipse) is probably is running from one location, and knows the directory structure to access your program files. 您的IDE(例如Eclipse)可能正在从一个位置运行,并且知道访问程序文件的目录结构。

The easiest, quickest solution is to just write the fully-qualified file path for Program.java . 最简单,最快捷的解决方案是为Program.java编写完全限定的文件路径。

The alternate is to find out what the current directory is. 另一种方法是找出当前目录是什么。 So, perhaps run pwd the same way you are running javac Program.java from within your program's code? 那么,也许运行pwd的方式与从程序代码中运行javac Program.java方式相同? Then you can see what directory your program is actually being run from. 然后,您可以看到您的程序实际运行的目录。 Once you know that, you can write the appropriate directory structure. 一旦你知道,你可以编写适当的目录结构。

For example, if pwd reveals that you are actually 2 directories above where Program.java is, then you can put those directories in the command like this: javac ./dir1/dir2/Program.java . 例如,如果pwd显示您实际上是Program.java所在的2个目录,那么您可以将这些目录放在命令中,如下所示: javac ./dir1/dir2/Program.java

To change the directory Eclipse runs from, see this question Set the execution directory in Eclipse? 要更改Eclipse运行的目录,请参阅此问题在Eclipse中设置执行目录?

您可以尝试在程序中打印路径,使用新文件(“。”)。getAbsolutePath(),如果您在ide中,路径可能位于项目的根目录而不是当前java的路径中文件

The reason my code was not working was because I was running it in Eclipse IDE, and that messes up the directory the program is running from. 我的代码无法工作的原因是因为我在Eclipse IDE中运行它,这会弄乱程序运行的目录。 To fix that program, I changed the command to javac -d . src/Program.java 为了修复该程序,我将命令更改为javac -d . src/Program.java javac -d . src/Program.java . javac -d . src/Program.java If I export the program into a .jar file and run it in the desktop, my original command will do just fine. 如果我将程序导出到.jar文件并在桌面上运行它,我的原始命令就可以了。

Thanks to saka1029 for helping me! 感谢saka1029帮助我!

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

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