0

I have created a class in the src folder of my project. When the user clicks a button on the main class, I want it to compile another class (random .java in the same project/src folder) into a jar file and save it to their c drive. Before compiling it, I need to also change a variable inside of the class thats being compiled.

Example:

package Whatever;
//All of my imports...
public static String replaceVar = "This variable in this class needs to be replaced from the other class before being compiled into a .jar on the users C drive!";
public class StubClass {
    public static void main(String[] args) {
        System.out.println(replaceVar);
    }
}

Need any help possible. Thanks in advance!

Justin G
  • 172
  • 3
  • 19
  • Main problem that I haven't seen during my research is replacing the variable before compiling it. I'll check it out, though. – Justin G Feb 09 '16 at 01:28
  • To replace the variable, you can use the String.replace method and replace the replaceVar to what you need it to be replaced with since you have the source code already. – Muhatashim Feb 09 '16 at 01:32
  • EDIT: Bad formatting. I'll try make the whole source code a string and use the method suggested on the other thread. I'll post my solution and results back here tomorrow. – Justin G Feb 09 '16 at 01:47
  • You can also load the whole source code into a string using Files.readAllBytes and initializing a new String with the bytes it returns. See http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – Muhatashim Feb 09 '16 at 02:04
  • Gets marked as a duplicate even though the other posts solution doesn't work for my problem :/ how unfortunate – Justin G Feb 12 '16 at 02:26

0 Answers0