简体   繁体   English

bat文件无法启动,并且bat文件位于文件夹中

[英]bat file does not start and bat file is in a folder

I am trying to launch a bat file. 我正在尝试启动bat文件。 The bat file is in a folder. bat文件位于一个文件夹中。 The folder contains all the executable jar file. 该文件夹包含所有可执行的jar文件。 I tried this code to launch the bat file but unable. 我尝试使用此代码启动bat文件,但无法启动。

    ProcessBuilder pb = new ProcessBuilder( "C:\\Users\\user\\Desktop\\NetBeansProjects\\Genomic DataWarehouse Project\\biodwh.startBioDWH.bat" );
    pb.redirectErrorStream(true);
      Process p = pb.start();
      InputStream is = p.getInputStream();
      BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
      for ( String line = br.readLine(); line != null; line = br.readLine() )
      {
              System.out.println( ">" + line ); 
      }
      p.waitFor();

try use this 试试这个
Runtime.getRuntime().exec("cmd /c start C:\\Users\\user\\Desktop\\NetBeansProjects\\Genomic DataWarehouse Project\\biodwh.startBioDWH.bat"); Runtime.getRuntime()。exec(“ cmd / c start C:\\ Users \\ user \\ Desktop \\ NetBeansProjects \\ Genomic DataWarehouse Project \\ biodwh.startBioDWH.bat”);

in your .bat add the line pushd %~dp0 在您的.bat文件中添加行pushd %~dp0

this will change the current drive and path to the one of the bat file. 这会将当前驱动器和路径更改为bat文件之一。

Ok, seems I misunderstood the question. 好的,看来我误解了这个问题。

I think you can't execute batch files directly but need to launch it using cmd.exe. 我认为您无法直接执行批处理文件,但需要使用cmd.exe启动它。 Try adding cmd /c (with a space at the end) to your new ProcessBuilder line : 尝试将cmd /c (末尾加一个空格)添加到新的ProcessBuilder行中:

ProcessBuilder pb = new ProcessBuilder( "cmd /c C:\\Users\\user\\Desktop\\NetBeansProjects\\Genomic DataWarehouse Project\\biodwh.startBioDWH.bat" );

Or ou can try to execute the bat file this way : 或者您可以尝试通过以下方式执行bat文件:

String path="C:\\Users\\user\\Desktop\\NetBeansProjects\\Genomic DataWarehouse Project\\";
File dir = new File(path);
Process process = Runtime.getRuntime().exec("cmd /c "+path+"biodwh.startBioDWH.bat", null, dir);

Or you can make it shorter if you don't need the batch file to be executed from the folder where it resides 或者,如果不需要从批处理文件所在的文件夹中执行批处理文件,则可以将其缩短

Process process = Runtime.getRuntime().exec("cmd /c C:\\Users\\user\\Desktop\\NetBeansProjects\\Genomic DataWarehouse Project\\biodwh.startBioDWH.bat");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM