0

I am working on a java application, it’s has a trial version. I need to save all informations to activate the application in an xml file ( such as expired day and application ID ), i need to save this file in an absolute path that will be the same for all pc.

Can u help me please?

elmetni hamza
  • 221
  • 1
  • 12

2 Answers2

2

Don't. It's very bad practice to use absolute paths for anything. Why not save the XML file either to a folder specific to the user (if your licenses are bound to people), or to a folder relative to your application (if your licenses are bound to a machine, as your question seems to indicate)?

Lying Dog
  • 1,484
  • 2
  • 12
  • 19
  • the problem is , if i save it to a folder relative to application , what if someone remove the whole folder after the expired day and re-install applicaition , it will get a new trial version for same machine , and i dont want that to happen – elmetni hamza Oct 26 '14 at 14:17
  • how can i save it to a folder specific to the user ? like in "my documents " folder? – elmetni hamza Oct 26 '14 at 14:19
  • 2
    What if the user deletes a file in your fixed location? Then she will be able to create a new key even without uninstalling the application. There are ways around your problem, either by binding license keys to the machine (see License4J for example), or by ensuring a person cannot request multiple keys (by requiring a valid email address that hasn't been used before when generating a key for example). 'Security by hidden location' is a form of 'security by obfuscation' and is not a good idea. – Lying Dog Oct 26 '14 at 14:21
  • 1
    See http://stackoverflow.com/a/1570764/4134826 to learn how to save a file to a location specific to a user. – Lying Dog Oct 26 '14 at 14:21
  • is there a way to save it in a folder bound to a machine that can work across linux and windows? – elmetni hamza Oct 26 '14 at 14:27
  • 1
    A good way to deal with saving settings (and license information can be considered a 'setting') is to use the Java Preferences API (see http://stackoverflow.com/a/711360/4134826). It hides the platform depending aspects behind a clean interface. – Lying Dog Oct 26 '14 at 14:30
  • Notice that it probably would be easy to technically circumvent your protection for trivial version. – Basile Starynkevitch Oct 26 '14 at 20:27
0

I agree that using absolute paths may not be the way to go but to solve your problem you could get the name of the user like so

String username = System.getProperty("user.name");

From there you can use the windows file structure to get to the documents folder

String documentsFolder = "C:/Users/"+username+"/Documents";

With that directory path you can read and write to that folder. This folder structure would only apply to windows and would need to be changed for Mac or Linux machines, but the username can be obtained the same way.

Just a suggestion. If you are supporting windows machines only maybe you can create a registry with the start and end date of the application and use that as a reference point which will be consistent even if the application is uninstalled or re-installed. Also maybe creating a System variable with the date could be another way to go. Just a few thoughts.

Hope this helps