adesh singh
adesh singh

Reputation: 1727

how to update jar class files through programming

I have a jar file which consisting of multiple class files. Main class file's java file is like

 class Main
  {
  public void setUserType()
   {
    String utype="user";
   }

   }

now i want to update the Main class file with usertype "user" to "admin" and recreate the jar file can you suggest me how can i do this so that the new jar file should have usertype "admin" These changes should be done through programming not through editor like netbeans or eclipse

Upvotes: 0

Views: 80

Answers (1)

SMA
SMA

Reputation: 37023

Instead of hardcoding things, you should define your code to accept properties either via property file or via environment/System property or command line argument when you start your application like below:

class Main {
  public void setUserType(String value) {
     String utype=value;//or use System.getProperty("value");if you used -Dvalue=admin in command line for example.
  }
  public static void main(String args[]) {//see main accepts command line argument
      if (args.length == 0) {
          throw new IllegalArgumentException("Required parameters are missing");
      }
      Main main = new Main();
      main(args[0]);//just pass whatever you pass as system parameter when you start your application
      //if you dont want to pass string across multiple methods and classes just use System.setProperty("value", args[0]); and use it like System.getProperty("value") to access it from anywhere without actually passing this string across.
   }
}

And run it with user/admin as you like:

java Main admin 
java Main user

Upvotes: 2

Related Questions