-2

I am making my first executable crossplatform jar file from a bunch of java files that I have. Problem is, I need to read in some values from a txt file just outside my file. This was working well before, where I would plop down said txt just next to my .class file and call

javac myClass this.txt

now this doesn't work, and the output I expect right outside said class doesn't work either.

Any ideas? Thank you.

RaymondMachira
  • 344
  • 2
  • 5
  • 14
  • 2
    `java myClass this.txt` – NilsH Apr 19 '13 at 07:28
  • May be this link can help you http://stackoverflow.com/questions/10452825/including-a-text-file-inside-a-jar-file-and-reading-it – Lav Apr 19 '13 at 07:29
  • 1
    You need to show some code and/or error messages. – Thilo Apr 19 '13 at 07:33
  • Well my args[0] expects a file name. As for error, I get null pointer – RaymondMachira Apr 19 '13 at 07:38
  • Why not put the text file inside the Jar? This will work well if it is read only. What is in the text file (e.g. user license, program credits, default application options..)? BTW - be sure to add @Thilo (or whoever) to inform them of a new comment. – Andrew Thompson Apr 19 '13 at 07:46

2 Answers2

0

I found this as the answer:

Use the File reader class, as opposed to using a Scanner with a File ala

Scanner scan= new scanner(new File("foo.txt")); //doesn't work

Scanner scan= new Scanner(new FileReader("foo.txt")); //does;

Thank you.

RaymondMachira
  • 344
  • 2
  • 5
  • 14
0

I have a similar case: wanting my *.jar file to access a file in a directory next to said *.jar file. Refer to THIS ANSWER as well.

My file structure is:

./ - the root of your program
|__ *.jar
|__ dir-next-to-jar/some.txt

I'm able to load a file (say, some.txt) to an InputStream inside the *.jar file with the following:

InputStream stream = null;
    try{
        stream = ThisClassName.class.getClass().getResourceAsStream("/dir-next-to-jar/some.txt");
    }
    catch(Exception e) {
        System.out.print("error file to stream: ");
        System.out.println(e.getMessage());
    }

Then do whatever you will with the stream

Community
  • 1
  • 1
ddaaggeett
  • 149
  • 2
  • 10