0

In my java program I have a package called resources and there I reserve a log.ascii file where I save some details.To do so i use the method

.class.getResource("/resources") everything works okay when i test it on eclipse but when i build the jar it won't find the path.Do you know how to overcome this problem? Thank you for your time

    private static String retrieveSelectedWorkspace() throws FileNotFoundException{

    String temp[];      
    String str=Gui.class.getResource("/resources").toString();

    temp=str.split("/",2);

    FileReader fr = null;
        fr = new FileReader(temp[1]+"/log.ascii");

    Scanner scanner = new Scanner(fr);

    while(scanner.hasNextLine())            
        return scanner.nextLine().toString();

    return null;
}
}
Libathos
  • 3,340
  • 5
  • 36
  • 61

4 Answers4

1

getResource returns an URL. Don't toString it and do some ugly string concatenation with it. After building the JAR the URL is not a file system URL. While developing in Eclipse and running from there you still have files where this works.

And of course you can't write to a resource in a JAR.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
  • oh, so it's not possible to write into the resource of the jar?thank you i didn't know it – Libathos Mar 31 '13 at 22:00
  • You can. But not like you are writing to a commun file. – DiogoSantana Mar 31 '13 at 22:03
  • You _normally_ can't without tricks and you _never_ should (unless you are building a Java compiler and packager). You don't even know at runtime how a JAR is served so tricks will not work in some cases (try to write to a JAR served from an HTTP server). Using a JAR as a container for user files would be... very uncommon. – Hauke Ingmar Schmidt Mar 31 '13 at 22:06
1

I think your code could use Class.getResourceAsStream() instead (/resources/log.ascii needs to be packaged in the jar, or somewhere else on the classpath).

private static String retrieveSelectedWorkspace() throws FileNotFoundException {
    InputStream in = Gui.class.getResourceAsStream("/resources/log.ascii");
    Scanner scanner = new Scanner(in);
    while(scanner.hasNextLine())            
        return scanner.nextLine().toString();
    return null;
}

But as to your actual question, you can definitely grab a folder resource from a jar, as long as that folder is packed inside the jar.

Test.java:

class Test {
    public static void main(String[] args) {
        System.out.println(Test.class.getResource("/text.txt"));
        System.out.println(Test.class.getResource("/folder"));
    }
}

Command line. Create a file and a folder. Then try both running the code from a .class file and running the code from a .jar file.

C:\Temp>echo text > text.txt

C:\Temp>md folder

C:\Temp>javac Test.java

C:\Temp>java Test
file:/C:/Temp/text.txt
file:/C:/Temp/folder

Create the jar:

C:\Temp>jar cvf test.jar Test.class text.txt folder
added manifest
adding: Test.class(in = 530) (out= 347)(deflated 34%)
adding: text.txt(in = 7) (out= 9)(deflated -28%)
adding: folder/(in = 0) (out= 0)(stored 0%)

Run from the jar:

C:\Temp>java -classpath test.jar Test
jar:file:/C:/Temp/test.jar!/text.txt
jar:file:/C:/Temp/test.jar!/folder
Paul Grime
  • 14,970
  • 4
  • 36
  • 58
0

Try

getClass().getClassLoader().getResource("/resources")))

You can also do the whole thing in one line:

new Scanner(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("/resources/log.ascii"))).useDelimiter("\\A").next()
danieln
  • 4,795
  • 10
  • 42
  • 64
0

When you run on eclipse the log.ascii is a file on the filesystem, but when you package into a jar, the log.ascii is a entry inside a zip like file. That's why you are not getting it done.

You can create a temporary file or create the file on the temp dir specified by the system property java.io.tmpdir to make it work.

Community
  • 1
  • 1
DiogoSantana
  • 2,404
  • 2
  • 19
  • 24