简体   繁体   English

我在c#中执行.bat文件时无法获取输出

[英]can't get the output when I excute a .bat file in c#

I'm trying to call .bat file from c#. 我正在尝试从C#调用.bat文件。 my project name is here's the code MaestroStart, and the batch file has the same name. 我的项目名称是代码MaestroStart,批处理文件具有相同的名称。 they are both at the same directory. 它们都在同一目录中。

   private void button1_Click(object sender, EventArgs e)
        {
            Process p = new Process();  
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;            
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = @"C:\Program Files (x86)\mbmteks\Maestro\MaestroStart.bat";
            p.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\mbmteks\Maestro\MaestroStart\MaestroStart\bin\Debug";
            p.Start();

            //string output = p.StandardError.ReadToEnd();
            string output = p.StandardOutput.ReadToEnd();
            textBox2.Text = output;


            p.WaitForExit();

        }

but this is the only output that I'm getting : ("JAVA_HOME does not point at a JDK or JRE. Either set the JAVA_HOME environment variable or specify a JDK for your IDEA project."). 但这是我得到的唯一输出:(“ JAVA_HOME并不指向JDK或JRE。要么设置JAVA_HOME环境变量,要么为您的IDEA项目指定JDK。”)。 If the use this statement 如果使用此语句

string output = p.StandardError.ReadToEnd();

I got nothing on the screen. 屏幕上什么也没有。

I tried to reset the Jave-Home environment variable to all the directories that I'm working with, but nothing changes. 我试图将Jave-Home环境变量重置为我正在使用的所有目录,但是没有任何变化。

this is the code of the batch file: 这是批处理文件的代码:

@echo off
setlocal

Set JAVA_HOME=.\jre

if exist "%JAVA_HOME%\bin\java.exe" goto JavaFound
echo "JAVA_HOME does not point at a JDK or JRE.  Either set the JAVA_HOME environment variable or specify a JDK for your IDEA project."
goto End

:JavaFound
if exist "start.jar" goto StartJarFound
echo "start.jar was not found.  Check your Jetty installation or your app path."
goto End

:StartJarFound
%JAVA_HOME%\bin\java.exe -jar start.jar

:End

this is the output of the batch file when I run it directly: 这是我直接运行批处理文件时的输出:

WARNING: System properties and/or JVM args set. 警告:设置了系统属性和/或JVM参数。 Consider using --dry-run or --e xec 2014-03-11 13:35:51.964:INFO:oejs.Server:jetty-8.1.12.v20130726 2014-03-11 13:35:51.998:INFO:oejdp.ScanningAppProvider:Deployment monitor C:\\Pro gram Files (x86)\\mbmteks\\Maestro\\webapps at interval 1 2014-03-11 13:35:52.010:INFO:oejd.DeploymentManager:Deployable added: C:\\Program Files (x86)\\mbmteks\\Maestro\\webapps\\ROOT log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAp pender. 考虑使用--dry-run或--e xec 2014-03-11 13:35:51.964:INFO:oejs.Server:jetty-8.1.12.v20130726 2014-03-11 13:35:51.998:INFO:oejdp .ScanningAppProvider:Deployment监控器C:\\ Pro gram文件(x86)\\ mbmteks \\ Maestro \\ webapps,间隔为1 2014-03-11 13:35:52.010:INFO:oejd.DeploymentManager:Deployable已添加:C:\\ Program Files(x86 )\\ mbmteks \\ Maestro \\ webapps \\ ROOT log4j:WARN在org.apache.log4j.DailyRollingFileAp中没有这样的属性[maxFileSize]。 log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.DailyRollingFil eAppender. log4j:WARN org.apache.log4j.DailyRollingFil eAppender中没有此类属性[maxBackupIndex]。 2014-03-11 13:36:00.540:INFO:ROOT:Initializing Spring root WebApplicationContext 2014-03-11 13:36:00.540:INFO:ROOT:初始化Spring根WebApplicationContext

2014-03-11 13:36:03.274:INFO:ROOT:Initializing Spring FrameworkServlet 'springDi spatcher' 2014-03-11 13:36:03.647:INFO:oejdp.ScanningAppProvider:Deployment monitor C:\\Pro gram Files (x86)\\mbmteks\\Maestro\\contexts at interval 1 2014-03-11 13:36:03.671:INFO:oejs.AbstractConnector:Started SelectChannelConnect or@0.0.0.0:9999 2014-03-11 13:36:03.274:INFO:ROOT:初始化Spring FrameworkServlet'springDi spatcher'2014-03-11 13:36:03.647:INFO:oejdp.ScanningAppProvider:Deployment Monitor C:\\ Pro gram文件(x86) \\ mbmteks \\ Maestro \\ contexts at interval 1 2014-03-11 13:36:03.671:INFO:oejs.AbstractConnector:Started SelectChannelConnect or@0.0.0.0:9999

Is my workingdirectory correct? 我的工作目录正确吗? I mean, it should be the directory of my current project ?? 我的意思是,它应该是我当前项目的目录?

 //to excute bat file in java 
Runtime runtime = Runtime.getRuntime();
    try {
        Process p1 = runtime.exec("cmd /c GO.BAT", null, new File("D:\\Documents and        Settings\\Administrator\\Desktop\\bat"));
          InputStream is = p1.getInputStream();
     int i = 0;
        while( (i = is.read() ) != -1) {
           System.out.print((char)i);
        }
   } catch(IOException ioException) {
        System.out.println(ioException.getMessage() );
    }

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

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