简体   繁体   English

如何使用 .bat 文件运行 jar 文件及其多个外部属性文件?

[英]How to run a jar file along with its multiple external properties files using a .bat file?

I have a Maven project with three properties files under resources directory.我有一个 Maven 项目,在资源目录下有三个属性文件。

Existing scenario:现有场景:

  • At the moment, I create jar file in Eclipse through maven build with goal as "assembly:single" ensuring that the build tag in pom.xml is updated appropriately. At the moment, I create jar file in Eclipse through maven build with goal as "assembly:single" ensuring that the build tag in pom.xml is updated appropriately.
  • This gives me a jar file with all the dependencies embedded in it.这给了我一个 jar 文件,其中嵌入了所有依赖项。
  • I execute this jar file with dependencies using batch command java -jar myjarname.jar我使用批处理命令java -jar myjarname.jar执行此 jar 文件和依赖项

Desired scenario:期望的场景:

  • However, I would like to export my project as a jar file without dependencies using "Export" feature of eclipse and place the three properties files in the same directory as where the jar file resides.但是,我想使用 eclipse 的“导出”功能将我的项目导出为没有依赖关系的 jar 文件,并将三个属性文件放在与 Z68995FCBF432492D15484D04A9D2AC04 文件所在的目录相同的目录中。

What should be the command to pass my properties files as arguments to the java -jar myname.jar ?将我的属性文件作为 arguments 传递给java -jar myname.jar的命令应该是什么?

you have to prepare your program using runtime argument pass facility like below你必须使用运行时参数传递工具来准备你的程序,如下所示

public class Echo {
    public static void main (String[] args) {
      //  args[0] is argument1
       // args[1] is argument2
    }
}

and then update your batch file like below然后更新您的批处理文件,如下所示

java -jar myjarname.jar argument1 argument2 

you can see more example:你可以看到更多的例子:

class A{  
public static void main(String args[]){  

for(int i=0;i<args.length;i++)  
System.out.println(args[i]);  

}  
} 

execution command:执行命令:

compile by > javac A.java编译 > javac A.java
run by > java A sonoo jaiswal 1 3 abc由 > java 运行 > java A Sonoo jaiswal 1 3 abc

Output: sonoo
       jaiswal
       1
       3
       abc

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

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