0

hi i new in java and i try to run executable (exe) of vienna packge from eclipse java, i want it will get string and use this on the exe , and i want to save the output of the exe in txt file, how can i do it?

    String[] params = new String [2];
    params[0] = "C:\\Program Files (x86)\\ViennaRNA Package\\RNAup.exe";
    params[1] = "GHHI";   
    try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
        out.print(Runtime.getRuntime().exec(params));
    }

tnx

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Does this answer your question? [Run cmd commands through Java](https://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) – sɐunıɔןɐqɐp Nov 02 '19 at 11:16

2 Answers2

0

Runtime.getRuntim().exec(...); returns an instance of Process. Process has a method getOutputStream. Use this method to get the stream. Once you have the stream read from it.

import java.io.*;

public class Main {
    public static void main(String[] args) {
        String[] params = new String[2];
        params[0] = "C:\\Program Files (x86)\\ViennaRNA Package\\RNAup.exe";
params[1] = "GHHI";
        try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
            Process p = Runtime.getRuntime().exec(params);
            final InputStream inputStream = p.getInputStream();
            final BufferedInputStream bis = new BufferedInputStream(inputStream);
            final BufferedReader br = new BufferedReader(new InputStreamReader(bis));
            String line;
            while((line = br.readLine()) != null) {
                out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
SebastianK
  • 712
  • 4
  • 19
  • Hmm, strange. Works for me as expected (but I am on Linux). But nevertheless, @Daniele s proposal to use redirectOutput is more straightforward and should be used. – SebastianK Nov 02 '19 at 12:11
0

You can use ProcessBuilder.redirectOutput to write directly to file, eg.

import java.io.File;

public class Sample {
  private static final String EXE_FOLDER = "C:\\Program Files (x86)\\ViennaRNA Package";
  private static final String EXE_NAME = "RNAup.exe";

  public static void main(String[] args) throws Exception {
    File exeDir = new File(EXE_FOLDER);
    File exeFile = exeDir.toPath().resolve(EXE_NAME).toFile();
    File outFile = new File("out.txt");
    File errFile = new File("err.txt");

    System.out.println("exeDir="+exeDir);
    System.out.println("exeFile="+exeFile);
    System.out.println("outFile="+outFile.getAbsolutePath());
    System.out.println("errFile="+errFile.getAbsolutePath());

    String[] params = { exeFile.getAbsolutePath() , "GHHI" };
    ProcessBuilder proc = new ProcessBuilder(params).directory(exeDir);

    proc.redirectOutput(outFile);
    proc.redirectError(errFile);
    proc.start();
  }
}
Daniele
  • 2,672
  • 1
  • 14
  • 20
  • ok, so i do " String[] params = new String[] {"cmd.exe", "/k", "cd C:\\Program Files (x86)\\ViennaRNA Package\\"};" and get into dir but it doest open me and run – Shaked ast Nov 02 '19 at 11:09
  • well there s no need to call `cd` (and `cmd.exe`); the executable can be invoked directly. I changed the example to reflect the paths you are using. – Daniele Nov 02 '19 at 12:10
  • ok daniele i understend what you did now, and i start to run this but the outpot txt and the err txt is empty, i cant understand why – Shaked ast Nov 02 '19 at 12:28
  • Maybe a stupid question, but as both solutions do not output anything, I have to ask: Does the executable you call output something at all? – SebastianK Nov 02 '19 at 12:36
  • Yes it is supposed to work- I checked this with both `cmd,/k,dir` (it prints the folder content), and also on a sample C program that would print hello-world to the standard output. Maybe the executable you call doesn't print anything to the std output? What happens when you call it normally, from the cmd line? – Daniele Nov 02 '19 at 13:07
  • i check with "cmd,/k,dir" and this is work, but with my exe it doenst work, what could it be? – Shaked ast Nov 02 '19 at 13:20
  • but nothing save on the output file – Shaked ast Nov 02 '19 at 13:45