gpt4 book ai didi

java - 从包含 java 命令的 java 文件执行 .class

转载 作者:行者123 更新时间:2023-12-01 13:26:38 25 4
gpt4 key购买 nike

import java.io.*;

public class chk
{
String command;
public String getMsg(String fileName,File Path1)
{
String dir,name=" ";
int x;
x=fileName.indexOf(".class");name=fileName.substring(0, x);
command ="java " + name +" < C:\\iptest\\input.txt > C:\\outtest\\"+name+".txt";
String output = executeCommand(command,Path1);
if(output.compareTo("")==0)
output = "Compilation Successfull!!";
return output;
}
private String executeCommand(String command,File Path1)
{
StringBuffer output = new StringBuffer();
Process p;
try
{
p = Runtime.getRuntime().exec(command,null,Path1);
//p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader1.readLine())!= null)
{
output.append(line + "\n");
}
while ((line = reader2.readLine())!= null)
{
output.append(line + "\n");
}
} catch (Exception e)
{
e.printStackTrace();
}
return output.toString();
}
public static void main(String args[])throws IOException
{
String x;
File dir=new File("C:\\Users\\RONEET\\Desktop");
chk ob=new chk();
x=ob.getMsg("hello.class",dir);
System.out.println("OUtput : "+x);
}
}

我在这个文件中所做的就是从 java 文件执行 hello.class 文件,并将其输出作为 txt 文件存储在以下位置 C:\outtest\ 中,并使用适当的文件名。但是当我编译上面的文件时,我的程序进入某种无限循环并且永远不会终止。

窗口保持这样 enter image description here

编辑:你好.java

import java.io.*;
class hello
{
public static void main(String agrs[])throws IOException
{
BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
String str;
str=s.readLine();
System.out.print(str+"\n");
}
}

最佳答案

免责声明 - 我发现我的解决方案可以工作,除非我使用带有 < 和 > 的 STDIN 和 STDOUT 重定向。因此,我使用此处提供的相同解决方案 Running external program with redirected stdin and stdout from Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Runner {

public static void pipeStream(InputStream input, OutputStream output) throws IOException {
byte buffer[] = new byte[1024];
int numRead = 0;

do {
numRead = input.read(buffer);
output.write(buffer, 0, numRead);
} while (input.available() > 0);

output.flush();
}

public static void main(String[] argv) {
File dir = new File("C:\\Users\\Leo\\workspace\\STackOverflow\\src\\");
FileInputStream fileIn = null;
FileOutputStream fileOut = null;

OutputStream procIn = null;
InputStream procOut = null;

try {
fileIn = new FileInputStream(new File(dir, "input.txt"));
fileOut = new FileOutputStream(new File(dir, "output.txt"));

Process process = Runtime.getRuntime().exec("C:\\jdk1.7.0_51\\bin\\java Hello", null, dir);
procIn = process.getOutputStream();
procOut = process.getInputStream();

pipeStream(fileIn, procIn);
pipeStream(procOut, fileOut);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}

给定

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Hello {

/**
* @param args
*/
public static void main(String[] args) {
try{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));

String input;

while((input=br.readLine())!=null){
System.out.println("processed"+input);
}

}catch(IOException io){
io.printStackTrace();
}

}

}

这与

几乎相同
"java Hello < input.txt > output.txt"

给定 input.txt 就像

1
2
3
4

它生成像这样的output.txt

processed1
processed2
processed3
processed4

关于java - 从包含 java 命令的 java 文件执行 .class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21785626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com