2

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.

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.

Is this possible? I've heard about ANT Scripts, but I'm not really sure if that's what I'm looking for here.

Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Austin
  • 4,801
  • 6
  • 34
  • 54
  • 3
    Check [this](http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file), there you may find all the info you need to create a jar programmatically – higuaro Oct 22 '12 at 03:59
  • @h3nr1x Thanks for that, but how would I go about changing something in a class before it creates the jar? – Austin Oct 22 '12 at 04:02
  • The below link might help you http://stackoverflow.com/questions/928896/dynamically-generate-jar-files-based-on-package-name-with-ant – Ruser1510890 Oct 22 '12 at 04:05
  • 2
    *"replaces some constants in a class with their settings."* Not very 'constant' then, are they? ;) I feel a better approach to this is to write the values to a property file that is in a place accessible to both the app. and the end user. That would be a path something like `${user.home}/settings/com/our/app/properties.prop` – Andrew Thompson Oct 22 '12 at 04:05

1 Answers1

4

have you considered using a .properties files or something similar instead? 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.

Check this page: http://www.mkyong.com/java/java-properties-file-examples/ which has some detail about saving to/loading from a properties file. 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.

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.

Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35
  • And I imagine I can store this in the JAR with the link from h3nr1x, and then use it like that. Thanks :) – Austin Oct 22 '12 at 04:20
  • @AndrewThompson I mean creating the properties file and storing it in a Jar. The properties file will not be changed, just read from. So from the question you posted, it seems I can do this then, correct? Thanks. – Austin Oct 22 '12 at 04:34
  • Hi Austin, I suppose you could put it in the jar if it was not read only (as Andrew pointed out, this tends not to be the case), but it would normally be better to keep the properties file in a set location outside the jar, such as in the user home folder – Chris O'Kelly Oct 22 '12 at 04:38
  • 2
    Just to build on that, I am getting the feeling that the idea of what you are building is that people use your GUI to output a jar that they can put on a removable disk or email to themselves, then use. Is that close to the case? If so, then an entirely new jar probably is the solution (IE, you have you gui application as well as some other application you've made, the GUI creates a jar with the user's chosen settings, they toddle off with it to their workstation, never to modify the settings again). If the idea is to allow users to change settings of the one application (using that app) – Chris O'Kelly Oct 22 '12 at 04:48
  • 1
    ...Then a properties file in the user home is the way to go – Chris O'Kelly Oct 22 '12 at 04:49