当前位置: 动力学知识库 > 问答 > 编程问答 >

java - can't get the output when I excute a .bat file in c#

问题描述:

I'm trying to call .bat file from c#.

my project name is

here's the code MaestroStart, and the batch file has the same name. 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.").

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.

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. 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.

log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.DailyRollingFil

eAppender.

2014-03-11 13:36:00.540:INFO:ROOT:Initializing Spring root 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

[email protected]: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() );
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: