this.getClass().getResource(“”).getPath() returns an incorrect path

后端 未结 7 888
灰色年华
灰色年华 2021-02-05 17:45

I am currently making a small simple Java program for my Computer Science Final, which needs to get the path of the current running class. The class files are in the C:\\2013\\g

相关标签:
7条回答
  • 2021-02-05 18:13

    Print out absolute path for a file in your classpath i.e. build/resources/main/someFileInClassPath.txt Disclaimer, this is similar to another solution on this page that used TT.class..., but this did not work for me instead TT..getClassLoader()... did work for me.

    import java.io.File;
    
    public class TT {
    
        /**
         * @param args
         */
        public static void main(String[] args) {    
            String path = TT.getClassLoader().getResource("someFileInClassPath.txt").getPath();
            File file = new File(path);
            System.out.println(file.getAbsolutePath());
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题