所以昨天从StackOverflow的一些帮助,这是赞赏,太糟糕了,我不能upvote呢。那么我能够在Java程序中创建一个Java程序,这更接近于让我完成计划中非常困难的部分。获取ProcessBuilder = new ProcessBuilder();工作,请帮忙
因此,现在作为标题状态,我有这个代码,我正在尝试执行另一个Java程序,但迄今为止我尝试过的一切都失败了。我现在有多个文件,没有错误,没有任何反应,没有打开。 Runtime.getRuntime()。exec();和ProcessBuilder = new ProcessBuilder();我也想知道我是否只能执行.jar文件,或者我是否也可以编译和执行.java文件?如果是这样如何? 所以是的任何帮助什么是坏的将不胜感激。
谢谢。
编辑:更新了代码,我似乎越来越接近它的工作。目录现在似乎工作。只要我得到建在回答它不能找到Java的罐子
代码我迄今可用后阅读下面的错误系统:
import java.io.File;
import javax.swing.JOptionPane;
public class SelfWrite {
public static void main(String args[]) {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
try {
String newLine = new String(System.getProperty("line.separator"));
File f = new File("C:/Users/Powermaniac/workspace/Newfolder/");
String fileDir = new String (f.getAbsolutePath());
File j = new File("C:/Windows/System32/java" + "-jar");
String javaDir = new String(j.getAbsolutePath());
File tempDir = new File (fileDir);
File tempjavaDir = new File (javaDir);
if (tempDir.exists());
JOptionPane.showMessageDialog(null, "File Exists");
if (tempjavaDir.exists());
JOptionPane.showMessageDialog(null, "Java Exists");
if (tempDir.exists()) {
String program = new String(fileDir + "UserInput.jar");
try {
ProcessBuilder pb = new ProcessBuilder("C:/Windows/System32/java", "-jar", program);
pb = pb.directory(new File(fileDir));
File temp = pb.directory();
String currentWorkingDirectory = "Current working directory: " + temp.toString();
JOptionPane.showMessageDialog(null, currentWorkingDirectory);
Process p = pb.start();
int UserInputExitCode = p.waitFor();
if (UserInputExitCode == 0) {
JOptionPane.showMessageDialog(null, "User Input Running Successfully!");
}
else {
JOptionPane.showMessageDialog(null, "User Input Exit Code: " + UserInputExitCode +
newLine + "Something went awry during UserInput.jar execution!");
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "User Input installation failed!" + newLine +
"Couldn't find \"Java -Jar\" in the Standard Software directory.");
}
}
else {
JOptionPane.showMessageDialog(null, "Can't find the Java -Jar directory!" +
newLine + "Program will now exit.");
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Something has gone awry while running the processbuilder class!");
}
}
}
变化JOptionPage.showMessageDialog为System.out.println,在catch(例外五){做电子商务.printStackTrace();所以它会打印正在发生的异常,之后您可以识别错误并尝试继续前进 – fmodos
我不相信'String fileDir = new String(System.getProperty(“C:/ Users/Powermaniac/workspace /用户输入/“));'和'String javaDir = new String(System.getProperty(”C:/ Windows/System32“)+”java -jar“);'要返回有效信息 – MadProgrammer
MadProgrammer,我将如何去查看什么返回有效的目录或文件?我基本上只是Java编程的新手,过去我所做的事已经被人遗忘了。不管怎样,谢谢。 – Powermaniac