简体   繁体   English

在Java中设置bat变量并执行/调用该bat

[英]Setting bat variables in java AND executing/calling that bat

I have 3 Variables in my java class. 我的java类中有3个变量。 I want to set this variables in a bat file and after that execute/call that bat. 我想在bat文件中设置此变量,然后执行/调用该bat。

before I used this bat for that process 在我把蝙蝠用于那个过程之前

Config.bat Config.bat

set PM_Drive = Whatever
set SWG = Whatever
set TOMCAT_HOME = Whatever
call nextbat.bat 

nextbat.bat needs these variables to work. nextbat.bat需要这些变量才能起作用。

How can I do this in java so I don't need config.bat anymore? 我该如何在Java中执行此操作,以便不再需要config.bat

I tried playing around with ProcessBuilder and Runtime but I cant figure it out. 我尝试使用ProcessBuilder和Runtime,但无法弄清楚。 Maybe someone can help me. 也许有人可以帮助我。

This is my progress so far which obviously does not work. 到目前为止,这是我的进步,显然是行不通的。

    public void connectTOMCAT(String SWG, String PM_DRIVE, String TOMCAT_HOME) {
    String[] cmdline = {"cmd", "/c", "start cmd.exe", "set SWG", "set PM_DRIVE", "set TOMCAT_HOME" };
    try {;
        Runtime.getRuntime().exec(cmdline);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I'm not an expert in batch script but I imagine your BAT file contains a call to the JVM like this 我不是批处理脚本方面的专家,但我想您的BAT文件包含对JVM的调用,如下所示

java -jar my_jar.jar

so you could pass your variables as arguments like this 这样就可以将变量作为这样的参数传递

java -jar my_jar.jar my_first_arg my_second_arg

and retrieve them in your Main method inside your java program. 并在Java程序内的Main方法中检索它们。

And if you want to pass these variables as arguments of your bat call, I guess you could do something lihe this : 如果您想将这些变量作为bat调用的参数传递,我想您可以这样做:

java -jar my_jar.jar %1 %2

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

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