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

后端 未结 7 1007
灰色年华
灰色年华 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:06

    Try this.

    import java.io.File;
    
    public class TT {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            String path = TT.class.getResource("").getPath();
            File file = new File(path);
            System.out.println(file.getAbsolutePath());
    
        }
    
    }
    

提交回复
热议问题