How to build JAR files dynamically - java

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

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.

Related

NetBeans save edited .jar

I am currently using a library for "Notify My Android". The library is using an outdated URL so i tried to change it. I attached the source file and now I can edit the code. Before attaching the source file it just said "compiled code". But when i save it it does not seem to save the changes. It is still using the old URL. Also the change I made is underlined in blue. I hope somebody knows how to make the .jar to accept my changes.
Thanks in advance
it's highly discouraged to modify jars you depend on simply because if you ever want to upgrade versions you'd need to modify the new jar you are looking for.
In those situations you have these options:
if it is an open source project, contribute to the project and correct the URL
try and set the property from your code (this may not be possible in certain situations)
try and extend the class you're trying to use and set the URL on the property you need (like the previous one, it may not be possible to do this)
this should be your last resource: create your own project (from the original jar), make the changes you require, package it up and add it to you app.

Eclipse custom builder: Identifying and cleaning generated files

I am creating a plugin for Eclipse, which contains tools for creating a custom type of project. These projects have a custom nature and builder. My builder (implements IncrementalProjectBuilder) takes a single input file, and generates a few (usually between 3 and 5) output files. When I run Clean Project, I need to remove the files the builder has previously generated.
Problem 1: The names of the generated files are not known exactly, but I do know the sort of files I expect to find (e.g. I know the extensions, and partial file names).
Problem 2: The user may add their own files to the project, which should not be affected by my build / clean steps.
My initial attempt was naive: remove every file except from the input file. This works, but has obvious problems.
My second attempt was better: I came up with a list of possible file names that may be generated, see if any of them exist and remove them.
By only knowing partial file names and matching them, I may inadvertently delete a user's file. E.g. I know I will generate a file called *_file.py. If the file I generate is called abc_file.py and the user has added their own xyz_file.py, I want to clean (remove) abc_file.py but leave xyz_file.py untouched.
The program which generates the output files from the input is constantly changing, and I don't want to rely on a concrete list of files that would need constant maintenance.
So, my question comes down to this. What methods exist for identifying the files generated by my custom builder, so I can remove them during a clean?
I've spent a couple of days Googling this one with not much to show for it. I am vaguely aware of a file system watcher in Java (Java7 WatchService?), but I don't know if that's the best solution to this problem.
Any information, advice or ideas appreciated.
One brute force approach would be to compare the project before and after the other program is invoked to get the list of files that were created/generated. Of course, it would be ideal if that program could somehow tell you which files it created. Once you have that list, you could iterate over those files as IFile's as use the setDerived() method to mark them as not being source files. When it comes time to clean the directory, you could use the derived setting to decide which files can be deleted.

C++ or Java Library for automated code editing?

I am writing/planning to write a program that takes in a java file (or multiple java files), and edits and adds functions/classes/variables and then outputs a new java file (or multiple files).
Is there a C++ or Java library that
Can recognize and output names of classes/functions within a text file
Can recognize and output the names of the input arguments for said classes/functions
Can allow me to insert code at specific lines or within specific functions
Can search for a given variable name/value
Maintains original file formatting
I would prefer not having to manually code something to do the above, so any help would be appreciated.
Thank you in advance!
EDIT: I currently use Eclipse, and am unsure of how to proceed. So to further explain my question:
In eclipse, if I write a program that opens another .java file, How would I go about 'asking' eclipse to output, say, all the class names of the .java file I just opened?
Also I will explain the 'purpose' of this project to further clarify. I want to write a program that can take in any java file, and turn it into a class that can implemented remotely via RMI. To do this I will need to add an execute() function, have the file implement Task and Serializable and add a few variables, etc... Based on my knowledge, doing this in Eclipse would require manual editing of the program, but I would like to completely automate this process.
Thank you, again.
Much of what you need can be found in a modern IDE; and some very good IDEs are open source (eclipse and Intellij IDEA Community Edition for Java). You might look there to see if there are modules that suite your needs.
Looks like you are talking of a tool like eclipse. You might not be looking for a full fledged IDE, but the requirements that you have mentioned are fulfilled by any basic IDE.
If you wish to make one of your own, you can do that using eclipse rich client platform.
All that you would need from Java is the reflection API.

How to correctly make app structure to use resources?

can anyone tell me how to make directory structure of desktop app, to proper use of resources? Here is example of my app structure (using maven). It worked until I tryied to change packages structure, I only renames folders.
New structure:
src/main/java/com/example/appname/app/App.java //main class with Application, just runs gui
src/main/java/com/example/appname/gui/GuiFrame.java //JFrame
now I have resources this way, but it doesn't work:
src/main/resources/com/example/appname/app/resources/App.properties
src/main/resources/com/example/appname/gui/resources/GuiFrame.properties
after clean and build, netbeans makes me:
target/classes/com/example/appname/app/App.class
target/classes/com/example/appname/app/resources/App.properties
target/classes/com/example/appname/gui/GuiFrame.class
target/classes/com/example/appname/gui/resources/GuiFrame.properties
But when I run it, on Swing controls I don't see any text, which is inside .properties file, they are empty.
may I set something somewhere? Thank you for answers.
Maybe after your refactoring you need to change the way you load resources, i.e. from getResourceAsStream("/App.properties") to getResourceAsStream("App.properties")?

Save something into JAR

I have a java app, that needs to save and load user settings. I want to save them in a file located in the JAR file, how could I achieve this?
That's not possible. Rather consider using java.util.prefs.Preferences which is designed for exactly this purpose. See also this little guide.
This is not a sensible course of action.
A JAR file is basically just a ZIP file. To rewrite its contents you need to extract them in full, make changes as needed and then write them to a new file that replaces the old one.
If the program that is going to do this is the same one as that contained in the JAR file, this becomes impossible as the file is write protected during execution.
You'd be better advised to store your configuration elsewhere.
That is not the way to store preferences as others said.
If you has to do it that way then :
Locate the JAR from code: How to get the path of a running JAR file?
Unjar the contents to temp folder
Modify in the temp folder
Jar temp folder to the new JAR file.
To add to what Kris said, most security experts will tell you that it's generally a bad security practice to allow end-user applications to modify their own code. What you're asking for would require that.

Categories

Resources