• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

changing the main class hardcoded values programatically with out extra file

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam having only one main class and I've hardcoded the values like password in the main class file.Then we are giving the option of changing password for the user.

The obvious way is to change the value of password variable ,but it works for that instance of running program. There are ways like configuaration files,Serialization .But they create one more extra file. Is it possible to do with one and only one main class.

What should i do to reflect in every execution?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand exactly when the value needs to be changed, but can't you pass it in as a command-line parameter?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other options include....

- Having the program get the password from the command line
- Having the program get the password from an environment variable
- Having the program get it from the user (via prompt)


There is not magic here. If you don't want to store this password in the program, or in an external file, it has to be store somewhere else.

Henry
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam not asking how to get the password from the user for comparison.I mean

If i store the password in the code,
like strPass="PASSWORD";

now i given option of changing the password like

strPass = "SomeOtherPassword";

Now you terminate the Program,you lost the changes you made because those are the memory values and you didn't dump them to your program.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
something like



User changed the password and the program terminated,

You start the program again,the password again the original not the one user changed.

How we can solve this by not storing the password anywhere else other than the code it self?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In theory, you could find the .class file and change the data in the class file itself. There are several different ways you might accomplish this, but all of them are pretty silly.

If you package your application in a jar file, then you might consider storing the data in a file in the jar file, and then having the program update the jar file. This is still silly, but maybe a little less so.

And of course, both of these schemes assume your .class or .jar file is writeable; what if it's stored on a CD?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE]Now you terminate the Program,you lost the changes you made because those are the memory values and you didn't dump them to your program.[

Once your program terminates and you want to retain value you have to persist it some where.

In theory, you could find the .class file and change the data in the class file itself


And modifying class file is surely not the best idea, plus the medium should be writable.

IMHO Creation of an extra file, using some existing file to store this change is the only elegant and standard way of doing this.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Storing the password in the class isn't just dangerous, but stupidly dangerous. If you rely on the obscurity of burying the password into the class, you're just welcoming disaster. There are too many better places to store it that I almost wish to just leave this topic alone.

2. If you really must use this, the only sane solution is to use ASM (http://asm.objectweb.org/) to re-engineer the class to which will contain the new password (not even close to simple thats for sure) then use JRE 1.5+'s java.lang.instrument package to redefine the runtime instance of the class so that you know it worked. Once re-engineered and redefined successfully, you can then attempt to write the byte array over the original class file if you can find it. Hint: start with MyReallyBadIdea.class.getClassLoader().getResource(MyReallyBadIdea.class.getName().replace(".", "/") + ".class").

3. Finally, this is the worst solution to any problem you could possibly have and you're in for a world of hurt trying, even if its just for fun.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've taken password as an example for changing code...it may look stupid .

I've seen win32 applications like this which doesn't store passwords anywhere ( Registry,property pages,configuration files....etc).I think its possible because a win32 valid executable can contain resource files.

At machine level , a program is combination of code i.e instructions and DATA,so can't we change the data i.e state of the program

Reverse Engineering the class file and changing resources like text strings are certainly possible.But,what i am looking is programatical change....

Anyway thanks for your suggestions....
[ March 01, 2008: Message edited by: ramya sri ]
 
Bras cause cancer. And tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic