简体   繁体   English

如何动态构建JAR文件

[英]How to build JAR files dynamically

I'm making a program that needs to be able to let Clients change a setting, and using what I'm calling a "Builder", create a .jar that replaces some constants in a class with their settings. 我正在制作一个程序,该程序需要能够让客户端更改设置,并使用我所谓的“生成器”创建一个.jar,用其设置替换类中的某些常量。

In other words, I have a GUI that has a few textfields so that when they press the JButton labeled Build, it creates a new Runnable Jar that in a Constants class whose settings are changed with what was in the textfields. 换句话说,我有一个具有几个文本字段的GUI,以便当他们按下标有Build的JButton时,它会在Constants类中创建一个新的Runnable Jar,该类的设置随文本字段中的内容而改变。

Is this possible? 这可能吗? I've heard about ANT Scripts, but I'm not really sure if that's what I'm looking for here. 我听说过ANT脚本,但是我不确定这是否是我想要的。

Thanks 谢谢

have you considered using a .properties files or something similar instead? 您是否考虑过使用.properties文件或类似文件? You can use ant scripts for what you are describing (check out http://ant.apache.org/manual/Tasks/replaceregexp.html , you could use this task in your build.xml to dynamically change the .java files but it seems a little kludgy) but it might not be the best solution. 您可以使用蚂蚁脚本来描述(查看http://ant.apache.org/manual/Tasks/replaceregexp.html ,可以在build.xml中使用此任务来动态更改.java文件,但是似乎有点笨拙),但这可能不是最佳解决方案。

Check this page: http://www.mkyong.com/java/java-properties-file-examples/ which has some detail about saving to/loading from a properties file. 检查此页面: http : //www.mkyong.com/java/java-properties-file-examples/ ,其中包含有关保存到属性文件或从属性文件加载的详细信息。 You could set up your constants class to load it's state variables from this file, and set up the Build JButton to create that properties file. 您可以设置常量类以从此文件中加载其状态变量,并设置Build JButton来创建该属性文件。

I'm trying to think of a use case where you would want to modify the class source itself rather than use a properties file, but to be honest I can't. 我正在考虑一个用例,在该用例中,您想修改类源本身而不是使用属性文件,但是老实说我不能。 So I suppose you may have some special circumstance where this is not a tenable solution for you, but 99% of the time this is how I would suggest you go about it. 因此,我想您可能会遇到一些特殊情况,这对您来说不是一个可行的解决方案,但是在99%的情况下,这是我建议您解决的方法。

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

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