2

I use eclipse luna on both windows8 64 bit and windows vista 32 bit. Its the same problem. My program run well in eclipse IDE but when compiled I get this exception:

c:\users\Preben\Desktop\test\src\resource\default.bws (path not find)

where 'test' is a folder on desktop and 'default.bws' is the file to deal with.

'resource' folder also exists as a subfolder in the projects bin folder.

In .classpath file is this line 'classpathentry kind="src" path="src"/'.

In eclipse 'project->properties->Java Build Path' in tab 'Source' is the line 'projectname/src'.

From time to time I have googled for hours to find a solution. Could anyone point me in the right direction?

pellebye
  • 21
  • 3

3 Answers3

1

As long as the file is accessible from your classpath, you can easily grab it with classLoader instead of using absolute paths (which is always a bad idea)

InputStream in = ClassPathTest.class.getClassLoader().getResourceAsStream("default.bws");

azraelAT
  • 752
  • 5
  • 9
0

Does "when compiled I get this exception" mean that it causes an error when the compiled program is executed from the command line?

If so, I doubt it is about how to specify the path in Windows, and may suggest:

  • Not to use desktop, but use a simple directory as the project location path such as C:\Project without including any space.
  • Verify the path specification to the default.bws file in the program. May use /Project/src/resource/default.bws instead of the Windows path format. The Java runtime should regards C:\ as /.
mon
  • 18,789
  • 22
  • 112
  • 205
  • Thank you for your answer. I get the exception in my log file. In my example the compiled program (BridgeBuddy.jar) is in folder 'test' on the desktop. I expect connection to the resource folder 'resource' to be possible in the compiled .jar file ... but it is not. – pellebye Jan 13 '15 at 21:04
  • forward slashes '/' work when running in Eclipse IDE. But after compilation I get the usual exception and it even still contains back slashes '\' – pellebye Jan 14 '15 at 09:44
  • Please refer to below. I believe it is a known question about hot to access a resource in a jar file (as answered by azraelAT). http://stackoverflow.com/questions/20389255/java-reading-a-resource-file-from-within-jar http://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file – mon Jan 14 '15 at 09:47
  • Thank you again. After a lot of reading from your links I finally find out that azraelAT had already given me the solution. Thank you! – pellebye Jan 15 '15 at 19:05
0

In conclusion I found that use of forward slashes in file address are important when connecting to resources:

public class TestOfForwardAndBackwardSlashes {

/**
 * Eclipse Luna and Windows 8.1: Test use of forward and backward slashes. 
 * Conclusion: Use forward slashes for resources.
 */
public TestOfForwardAndBackwardSlashes() {
    try {

        InputStream in = this.getClass().getClassLoader()
                // Forward slashes ok both in IDE and when compiled
                .getResourceAsStream("resource/default.bws"); 
                // Backward slashes only ok in IDE                              
                // .getResourceAsStream("resource\\default.bws");

        OutputStream out = new FileOutputStream(new File(
                // Forward slashes ok both in IDE and when compiled                 
                "C:/test/default.bws")); // Ok both in IDE and compiled
                // Backward slashes ok both in IDE and when compiled                    
                // "C:\\test\\default.bws"));

        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}

}

pellebye
  • 21
  • 3