Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a problem with paths as pointed out by techurbana.</p> <p>You need to set the working directory on your process by invoking the .directory(String) method of the process builder before starting the process. You need to give it the root directory of the application.</p> <p>You can get the current working directory of the JAVA executable (may or may not be where you store your mkvmerg.exe) by doing a:</p> <pre><code> File dir = new File(""); dir.getAbsolutePath(); </code></pre> <p>If your mkvmerg.exe utility has a class path entry in your system (on windows anyway) you should be able to run mkvmerg.exe from any directory.</p> <p>Then if you have the files you wish mkvmerge to work on in some sub folder say FolderA you do:</p> <pre><code> File subDir = new File(dir, "FolderA"); subDir.getAbsolutePath(); </code></pre> <p>Just try printing out all the paths you are using to console during runtime to verify that the paths to the files, and to the executables are correct, and debugging should be a snap.</p> <p>Please review the example of managing paths below. It just opens up files in the current directory with notepad, but is pretty much exactly what you want to do. To run this, pass some path as an argument to main().</p> <p>EDIT - I just realized that I never actually make use of the path passed into the application, and only ever look for the first file in the current directory of the java executable, but you get the point.</p> <pre><code> import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; public class MainTest { public static void main(String[] args) { // passes in a directory if (args.length &gt; 0) { doSomethingWithCMD(args[0]); } } public static void doSomethingWithCMD(String filesDir) { try { File currDir = new File(""); // this is a bit weird, but listFiles didnt return anything without // this next line. currDir = new File(currDir.getAbsolutePath()); System.out.println(currDir.getAbsolutePath()); // build our command with a string builder StringBuilder sb = new StringBuilder(); // get the list of files File[] files = currDir.listFiles(); // if there are no files this will be null so check first if (!(files == null)) { for (File f : files) { if (f.isFile()) { sb.append("notepad "); sb.append("\"" + f.getAbsolutePath()); sb.append("\""); break; } } } // create our process builder // first with no working dir &amp; absolute paths ProcessBuilder pb = new ProcessBuilder(sb.toString()); Process p = pb.start(); InputStream inputStream = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader( inputStream)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } // cleanup try { inputStream.close(); } catch (Exception ignore) { } try { br.close(); } catch (Exception ignore) { } sb = new StringBuilder(); // now we rebuild our command using relative paths instead of // absolute ones if (!(files == null)) { for (File f : files) { if (f.isFile()) { sb.append("notepad "); sb.append("\"" + f.getName()); sb.append("\""); break; } } } pb = new ProcessBuilder(sb.toString()); //set the working directory pb.directory(currDir); p = pb.start(); inputStream = p.getInputStream(); br = new BufferedReader(new InputStreamReader(inputStream)); line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload