0

I have a problem,

for the nature of my project, I need to read a txt file, packed in a JAR, this is my situation:

I have a project in eclipse, and I create a txt file into it: project->new->new txt file in this file I will store my login setting.

I want to create an executable JAR, and when I will execute this JAR, I want that use the data stored into the txt file.

This Jar should be as independent as possible, because I will need to execute it in many different places and situation, and I only can have one file (jar) for that, this txt should be into the jar.

How can I do that?

I hope this same question doesn't be answered before (I have been looking for that but I didn't find any response) if is a duplicated question I am apology in advance.

durron597
  • 31,968
  • 17
  • 99
  • 158

4 Answers4

0

You can't write to a file stored inside a JAR file.

You could store the JAR file and the text file together somewhere and use a batch file (.bat) to execute the JAR with the filename as a parameter.

This way you would have only one file to execute.

Could that do the job ?

Simon
  • 1,605
  • 13
  • 22
  • Thank you for your comment, but the jar will be executed by remote, and I don't know nothing about where the Jar will be stored, for that I don't want to have more files than a jar and I really don't know if I will can, in addition I won't be the person who manage this jar in the future, so probably this file with the settings, will be lost or whatever, one option is store this as variables hardcoded in the program, but this should be the "last option" (I'm so new with java, I don't know if I have more option" – Miguel Batuecas Feb 07 '13 at 17:58
0

You should look into "Class.getResourceAsStream(String name)" from core Java. You can use it to open a text file (or any other kind of file) from the application, packaged in a jar or whatever.

Be aware that, if you're storing sensitive information in the file, it can be viewed by anyone who has the jar; you need to encrypt the information if you don't want it looked at.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • ok, thank you I will try some with that, don't worry about the data stored is only some configuration, but I will keep it out of the code for edit it if will be necessary without edit the code – Miguel Batuecas Feb 07 '13 at 18:07
  • Be sure to accept answers that you judge to be the 'best' answers to your questions, by clicking on the checkmark left of the text of that answer. It's part of how StackOverflow works, and therefore makes it more likely you'll get answers to other questions you may ask later. – arcy Feb 16 '13 at 11:27
0

You can get your .jar's Location through

getClass().getProtectionDomain().getCodeSource().getLocation()

and from there you create a JarFile with this location, get the txt JarEntry and write it out via FileOutputStream. Then you can read in the contents of your file.

ZweiGraf
  • 11
  • 1
  • 2
  • 4
0

Thank you very much for all your answers, at the en I have resolved this issu with the nex instructino:

in = new BufferedReader(new InputStreamReader(AppTask.class.getClassLoader().getResourceAsStream(AppData.SettingsFile)));

It works fine. thank again, bye