Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过编程更新jar类文件_Java_Eclipse_Jar_Executable Jar - Fatal编程技术网

Java 如何通过编程更新jar类文件

Java 如何通过编程更新jar类文件,java,eclipse,jar,executable-jar,Java,Eclipse,Jar,Executable Jar,我有一个jar文件,它由多个类文件组成。 主类文件的java文件如下 class Main { public void setUserType() { String utype="user"; } } 现在我想用usertype“user”将主类文件更新为“admin”,并重新创建jar文件 你能建议我怎么做才能使新的jar文件的用户类型为“admin”吗 这些更改应该通过编程而不是像netbeans或eclipse这样的编辑器来完成,而不是硬编码。启动应

我有一个jar文件,它由多个类文件组成。 主类文件的java文件如下

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

   }
现在我想用usertype“user”将主类文件更新为“admin”,并重新创建jar文件 你能建议我怎么做才能使新的jar文件的用户类型为“admin”吗
这些更改应该通过编程而不是像netbeans或eclipse这样的编辑器来完成,而不是硬编码。启动应用程序时,您应该通过属性文件或环境/系统属性或命令行参数来定义代码以接受属性,如下所示:

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.
   }
}
并根据需要使用user/admin运行它:

java Main admin 
java Main user

为此修改代码是错误的做法。只需使用外部资源从中选取值,例如属性文件。正如@fge所说的,这是一种不好的做法。但是如果您确实需要它,您可以使用
Java反射API