pellebye
pellebye

Reputation: 21

eclipse source files are missing after java project is compiled

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?

Upvotes: 2

Views: 1687

Answers (3)

pellebye
pellebye

Reputation: 21

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();
    }
}

}

Upvotes: 0

azraelAT
azraelAT

Reputation: 762

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");

Upvotes: 1

mon
mon

Reputation: 22388

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 /.

Upvotes: 0

Related Questions