0

It works well "new FileInputStream(f.getAbsoluteFile())"

private byte[] loadFile(String path) throws IOException {
    File f = new File("./build/classes/" + path);
    try (InputStream is = new FileInputStream(f.getAbsoluteFile())) {
        byte[] data = loadFile(is);
        return data;
    }
}

And "new FileInputStream(f)"

private byte[] loadFile(String path) throws IOException {
    File f = new File("./build/classes/" + path);
    try (InputStream is = new FileInputStream(f)) {
        byte[] data = loadFile(is);
        return data;
    }
}

throw exception:

java.io.FileNotFoundException: ./build/classes/traces/onmethod/ErrorDuration.class (No such file or directory)

at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)

I can't imagine why.

saaav
  • 34
  • 4
  • because f and f.getAbsoluteFile() are not the same. – Stultuske Jan 10 '18 at 09:47
  • 1
    It would be helpful to show the absolute path `f.getAbsoluteFile()` – Erwin Bolwidt Jan 10 '18 at 09:50
  • Excuse me,where is the difference? @Stultuske – saaav Jan 10 '18 at 09:51
  • f.getAbsoluteFile() not getAbsolutePath,return a File object @Erwin Bolwidt – saaav Jan 10 '18 at 09:53
  • What do you get when you do `System.out.println(f.getAbsoluteFile().getPath())`? – DodgyCodeException Jan 10 '18 at 09:55
  • Reread my comment, I *said* getAbsoluteFile. Show the value. – Erwin Bolwidt Jan 10 '18 at 09:55
  • `System.out.println(f.getAbsoluteFile())` shows the absolute path. – Erwin Bolwidt Jan 10 '18 at 10:01
  • @ErwinBolwidt /home/aaa/Github/./build/classes/traces/onmethod/ErrorDuration.class – saaav Jan 10 '18 at 10:06
  • @DodgyCodeException System.out.println(f.getAbsoluteFile().getPath()) :/home/aaa/Github/./build/classes/traces/onmethod/ErrorDurati‌​on.class and real path : /home/aaa/Github/build/classes/traces/onmethod/ErrorDurati‌​on.class – saaav Jan 10 '18 at 10:10
  • 1
    Do you, at any time, call `System.setProperty("user.dir", ...)`? – DodgyCodeException Jan 10 '18 at 10:20
  • @DodgyCodeException I used "-Duser.dir=/home/aaa/Github/" in vm option – saaav Jan 10 '18 at 10:31
  • 2
    @saav https://stackoverflow.com/questions/36880692/java-file-does-not-exists-but-file-getabsolutefile-exists this link I got while reading more about getAbsoluteFile and this will resolve your issue – Rohan Kadu Jan 10 '18 at 10:34
  • What Rohan Kadu says is correct. You shouldn't change user.dir. It doesn't actually change the current working directory of your process; it just changes a system property which is used by `getAbsoluteFile()`. On the other hand, `new FileInputStream(File)` uses the *real* current directory to resolve the file. So instead of `-Duser.dir=`, do `cd /home/aaa/Github` just before you start the application. – DodgyCodeException Jan 10 '18 at 10:37
  • Thanks.It just a uint test,and there's a lot of this ,let me think about how to change it – saaav Jan 10 '18 at 10:57

1 Answers1

0

First make sure you run your two programs from the same working directory.

Also, in the first case, your path is resolved to an absolute path first. For example: /opt/local/myprod/bin/build/classes/traces/onmethod/ErrorDuration.class

Whle FileInputStream uses the 'simple path' of your file (File.getPath())

Depending on your current work directory, your permissions and your symbolic links those two paths can mean two different things.

From the directory you run your program - does this path work ?

ls ./build/classes/traces/onmethod/ErrorDuration.class

Also see: What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

Axel Podehl
  • 4,034
  • 29
  • 41