我有一个包含多个类文件的jar文件。 主类文件的java文件就像
class Main
{
public void setUserType()
{
String utype="user";
}
}
现在我想用usertype" user"更新Main类文件到"管理员"并重新创建jar文件 你能建议我怎样才能做到这一点,以便新的jar文件应该有usertype" admin" 这些更改应该通过编程来完成,而不是通过编辑器如netbeans或eclipse
答案 0 :(得分:2)
您应该定义代码,以便在启动应用程序时通过属性文件或环境/系统属性或命令行参数来定义代码以接受属性,如下所示:
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