【发布时间】:2014-02-14 17:21:04
【问题描述】:
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\ 并具有正确的文件名。但是当我编译上面的文件时,我的程序进入了某种无限循环并且永远不会终止。
窗口保持这样
编辑: 你好.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");
}
}
【问题讨论】:
-
我们可以看看你的
Hello类是做什么的吗? -
为什么一个Java程序调用另一个?直接使用
Hello类不是更有意义吗? -
@SotiriosDelimanolis 完成
-
@JohnGaughan 不同的应用程序有不同的要求我的需要这就是为什么我正在研究它
-
@rick 这就是我评论中第一个词的原因:“为什么”。我很好奇这个要求。也许有更好的方法?
标签: java swing command-line