title: Java-Application: Execute Batch File with subprocess / UNC Path
body: <p>I want to execute a batch file within my java application.</p>
  
  <p>I want to execute it this way:</p>
  
  <pre><code>   String[] args = new String[] {
                      "C:/Users/User1/Desktop/Bachelor Thesis/JDBC Connector/jdbc_5.2_sp1_patch05/bin/connect.bat",
                      "start",
                      "-f",
                      "C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/etc/db.xml" };
  
  Process p = Runtime.getRuntime().exec(args);
  p.waitFor();
  
              BufferedReader prout = new BufferedReader(new InputStreamReader(p.getErrorStream()));
              String buffer;
              while ((buffer = prout.readLine()) != null) {
                  System.out.println(buffer);
                  buffer = prout.readLine();
              }
  </code></pre>
  
  <p>For explaination:<br>
  Let us consider I would use a normal cmd execution. I would open cmd, then i would go to my directory:</p>
  
  <pre><code>cd C:/Users/User1/Desktop/Bachelor Thesis/JDBC Connector/jdbc_5.2_sp1_patch05/bin
  </code></pre>
  
  <p>Afterwars I would start the batch file with the following command:</p>
  
  <pre><code>connect start -f C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/etc/db.xml
  </code></pre>
  
  <p>On the command line this works fine, it starts the batch and inside this batch there is a subprocess wich gets started:</p>
  
  <pre><code>if exist %JAVA_HOME%\bin\java.exe (
  java -XX:NewSize=%NEWSIZE% -XX:NewRatio=1 -Xms%MAXHEAP% -Xmx%MAXHEAP% -Dfile.encoding=%ENCODING% -Djava.ext.dirs=%EXT_DIR% com.fastsearch.esp.connectors.jdbc.JDBCConnector %1 %2 %3 %4 %5 %6 %7
  ) else (echo Is JAVA_HOME=%JAVA_HOME% set correctly?)
  </code></pre>
  
  <p>But with my execution inside my Java application I have the following problem:
  It only prints the first echo of my batch file but the subprocess does not start. When I catch the errorstream (like shown in the code on top) it says:</p>
  
  <pre><code>UNC-Pfade werden nicht unterstützt. (UNC-Paths are not supported)
  java.lang.NoClassDefFoundError: com/fastsearch/esp/connectors/jdbc/JDBCConnector
  </code></pre>
  
  <p>After some reseach I found out that the cmd has problems to start subprocesses because of the current working directory or something like that. I did not really understand that.</p>
  
  <p>The actual thing is, that I want to execute the batch file with its neccesary parameters (start, -f and the path) within my Java application. When I execute the statements directly on the cmd everything works fine but inside my java application it sucks. </p>
  
  <p>Here is the full batch file, maybe this is helpful:</p>
  
  <pre><code>@echo off
  rem This bat file should setup the java env and run the connector manager
  SETLOCAL
  
  echo Copyright (c) Microsoft Corporation.  All rights reserved.
  
  rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  rem Java 1.6 must be installed and set here
  rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  if NOT defined JAVA_HOME (
     echo Sorry..
     echo You have NOT set JAVA_HOME - Should point to where JRE 1.6.x or later is installed
     echo Exiting.......
     goto end
  )
  set JAVA_HOME=%JAVA_HOME%
  :: Remove quotes if exist, then add quotes in case spaces
  SET JAVA_HOME=###%JAVA_HOME%###
  SET JAVA_HOME=%JAVA_HOME:"###=%
  SET JAVA_HOME=%JAVA_HOME:###"=%
  SET JAVA_HOME=%JAVA_HOME:###=%
  SET JAVA_HOME="%JAVA_HOME%"
  
  SET PATH=%JAVA_HOME%\bin
  SET EXT_DIR=..\lib;%JAVA_HOME%\lib;%JAVA_HOME%\lib\ext
  SET CLASSPATH=..\etc
  SET MAXHEAP=1408m
  SET NEWSIZE=256m
  SET ENCODING=UTF8
  
  if exist %JAVA_HOME%\bin\java.exe (
  java -XX:NewSize=%NEWSIZE% -XX:NewRatio=1 -Xms%MAXHEAP% -Xmx%MAXHEAP% -Dfile.encoding=%ENCODING% -Djava.ext.dirs=%EXT_DIR% com.fastsearch.esp.connectors.jdbc.JDBCConnector %1 %2 %3 %4 %5 %6 %7
  ) else (echo Is JAVA_HOME=%JAVA_HOME% set correctly?)
  
  :end
  </code></pre>