我一直在努力奔跑
C:\Users\Luis\Desktop\RESEARCHGUIPROJECT\ResearchGUI\CHEMKED\CHEMKED_SOLVER.exe
使用 java 或让 java 使用命令提示符运行此可执行文件。现在我相信我能够将 java 获取到该文件但是当它运行它时可执行文件给我 ERR IO-21 invalid STATUS specifier for给定文件。
我已经从 DOS 窗口运行了可执行文件,通常当给出该代码时,这意味着输入文件指定不正确。要了解求解器的背景,请从名为 solverfilepath.txt 的文件中读取,并在该文件中为该文件所在的位置指定一个文件目录。此文件是名为 SOLTMP.txt 的求解器输入。
此错误仅在我运行 java 时出现,但在我从命令窗口手动运行时不会出现。
我不知道是否有办法在 java 运行此程序时也让它打开命令窗口以查看在命令提示符中运行的可执行文件。
有什么建议吗?
int i = 1;
while(finalTime < 1.0000){
try{
// Execute CHEMKED solver
// Added 07/06/2013 from StackOverFlow
Runtime rt = Runtime.getRuntime();
String[] CHEMKED = {"C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe"};
Process pr = Runtime.getRuntime().exec(CHEMKED);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println("*****");
System.out.println(line);
}
int exitVal;
exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
try{
inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
}catch(Exception e){
e.printStackTrace(System.out);
}
try{
copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
}catch(Exception ex){
ex.printStackTrace(System.out);
}
//copy SOLTMP content to temporary file
while(inputFile3.hasNextLine()){
fileLine = inputFile3.nextLine();
copyFile.format("%s%n",fileLine);
}
copyFile.close();
inputFile3.close();
// Added 07/05/2013
initialTime+= 0.10;
finalTime+= 0.10;
// Added 07.03
updateFile();
i++;
}
到目前为止,这是我添加的内容,与在 javaWorld 上所做的类似。我还没有机会运行它,但只是看看我是否正朝着正确的方向前进。
class StreamGobbler extends Thread
{
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type)
{
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect)
{
this.is = is;
this.type = type;
this.os = redirect;
}
public void run()
{
try
{
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
if (pw != null)
pw.println(line);
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public void chemked(String args[]) {
int i = 1;
while(finalTime < 1.0000) {
if (args.length < 1)
{
System.out.println("USAGE java GoodWinRedirect <outputfile>");
System.exit(1);
}
try{
FileOutputStream fos = new FileOutputStream(args[0]);
String[] CHEMKED = { "C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe"};
Process pr = Runtime.getRuntime().exec(CHEMKED);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
// any error message
StreamGobbler errorGobbler = new
StreamGobbler(pr.getErrorStream(), "ERROR");
// any output
StreamGobbler outputGobbler = new
StreamGobbler(pr.getInputStream(), "OUTPUT", fos);
// start them off
errorGobbler.start();
outputGobbler.start();
String line=null;
while((line=input.readLine()) != null) {
System.out.println("*****");
System.out.println(line);
}
int exitVal;
exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
fos.flush();
fos.close();
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
try{
inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
}catch(Exception e){
e.printStackTrace(System.out);
}
try{
copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
}catch(Exception ex){
ex.printStackTrace(System.out);
}
//copy SOLTMP content to temporary file
while(inputFile3.hasNextLine()){
fileLine = inputFile3.nextLine();
copyFile.format("%s%n",fileLine);
}
copyFile.close();
inputFile3.close();
// Added 07/05/2013
initialTime+= 0.10;
finalTime+= 0.10;
// Added 07.03
updateFile();
i++;
}
}
告诉我谢谢。
解决方案:
int i = 1;
while(finalTime < 1.0000) {
try{
FileOutputStream fos = new FileOutputStream("C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\error" +i+".txt");
String[] CHEMKED = { "C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED\\CHEMKED_SOLVER.exe", "SOLTMP.txt"};
//Process pr = Runtime.getRuntime().exec(CHEMKED);
ProcessBuilder builder = new ProcessBuilder(CHEMKED);
builder.directory(new File("C:\\Users\\Luis\\Desktop\\RESEARCHGUIPROJECT\\ResearchGUI\\CHEMKED"));
builder.redirectError();
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(pr.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(pr.getInputStream(), "OUTPUT", fos);
// kick them off
errorGobbler.start();
outputGobbler.start();
int exitVal;
exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
fos.flush();
fos.close();
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
try{
inputFile3 = new Scanner(new FileReader("CHEMKED\\SOLTMP.txt"));
}catch(Exception e){
e.printStackTrace(System.out);
}
try{
copyFile = new Formatter(new File("CHEMKED\\output-ring1_"+ i +".txt"));
}catch(Exception ex){
ex.printStackTrace(System.out);
}
//copy SOLTMP content to temporary file
while(inputFile3.hasNextLine()){
fileLine = inputFile3.nextLine();
copyFile.format("%s%n",fileLine);
}
copyFile.close();
inputFile3.close();
// Added 07/05/2013
initialTime+= 0.10;
finalTime+= 0.10;
// Added 07.03
updateFile();
i++;
}
}
最佳答案
同样,考虑使用 ProcessBuilder 并将其工作目录设置为与执行文件相同的位置。像这样的东西:
String path = "C:/Users/Luis/Desktop/RESEARCHGUIPROJECT/ResearchGUI/CHEMKED/";
String app = "CHEMKED_SOLVER.exe";
List<String> command = new ArrayList<String>();
// command.add(path);
command.add(app);
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(new File(path));
processBuilder.redirectError();
Process process = processBuilder.start();
BufferedReader bReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bReader.readLine()) != null) {
System.out.println(line);
}
关于java - 从桌面运行可执行文件给我 : *ERR* IO-21 invalid STATUS specifier for given file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508348/