【问题标题】:executing a .class from a java file with java commands in it从包含 java 命令的 java 文件中执行 .class
【发布时间】: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


【解决方案1】:

免责声明 - 我发现我的解决方案可以工作,除非我使用带有 的 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 hello C:\outtest\hello.txt”命令?
  • 如果你能用我的这个命令编辑你的代码会很有帮助
  • 对不起,我赶时间:-(
  • 如果是这样,那么请帮助我 :-) 我欠债了
【解决方案2】:

流的读取可能是阻塞的。您可以尝试将流读取放入单独的线程中。

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 2014-12-06
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2015-04-01
    • 2012-06-23
    • 2013-08-25
    相关资源
    最近更新 更多