×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: userc865b
Added: Apr 11, 2018 8:07 AM
Modified: Apr 12, 2018 3:02 PM
Views: 3038
Tags: java
  1.        //Möglichkeit 1
  2.         Test test = new Test();
  3.         System.out.println(getClassResource(Test.class));
  4.  
  5.         //Möglichkeit 2
  6.         Class<?> c = Test.class;
  7.         String path = c.getResource(c.getSimpleName() + ".class").getPath().replace(c.getSimpleName() + ".class", "");
  8.        
  9.         System.out.println(path);
  10.  
  11.         //Möglichkeit 3
  12.        Path workingDirectory= Paths.get("").toAbsolutePath();
  13.         System.out.println(workingDirectory);
  14.  
  15.         //Möglichkeit 4
  16.         String workingDir = System.getProperty("user.dir");
  17.         System.out.println("Current working directory : " + workingDir);
  18.  
  19.  
  20.     private static String getCurrentFilePathSourceFile() {
  21.          Class<?> c = this.getClass();
  22.         String path = c.getResource(c.getSimpleName() + ".class").getPath().replace(c.getSimpleName() + ".class", "");
  23.  
  24.         Path workingDirectory= Paths.get("").toAbsolutePath();
  25.         String pathArray[]=workingDirectory.toString().split("\\\\");
  26.         String workingPackage=pathArray[pathArray.length-1];
  27.         path= path.replace("out/production/"+workingPackage,"src");
  28.         return path;
  29.     }
  30.  
  31.  
  32.        public String getClassFilePath() throws URISyntaxException {
  33.  
  34.         URL url=this.getClass().getResource(".");
  35.         File file= new File(url.toURI());
  36.         return file.getPath();
  37.     }