0

I have a file dateTesting.java . the path's directory is as follows: D:\workspace\Project1\src\dateTesting.java . I want the full path of this file as "D:\workspace\Project1\src" itself but when I use any of the following code, i get only "D:\workspace\Project1" . the src part is not coming.

System.out.println(System.getProperty("user.dir"));
File dir2 = new File(".");
System.out.println(dir2.getCanonicalPath().toString());
System.out.println(dir2.getAbsolutePath());

How can I get the full path as "D:\workspace\Project1\src" ? I'm using eclipse ide 3.5

Thank you

Tom
  • 411
  • 12
  • 28
  • 2
    Why do you want to do this? There may be other solutions – cowls Sep 17 '13 at 09:42
  • use File file = new File("com/yourpackage/dateTesting.java"); sysout(file.getAbsolouteFile()); – Suryaprakash Pisay Sep 17 '13 at 09:51
  • I will be using a single file for different projects with different src file name....so i want to get it dynamically. – Tom Sep 17 '13 at 09:51
  • @SuryaprakashPisay: that code also doesnt give the src file name – Tom Sep 17 '13 at 09:57
  • As of now I have done a work around. I search for the folders in Project directory then I match the folder name with regular expression and then take it. – Tom Sep 18 '13 at 04:55

5 Answers5

0

dateTesting.java is a Java source file which is not available after compilation to bytecode. The source directory it was in is not available, too.

dir2 is the File of the directory you execute the .class file in. It seams that this happens to be D:\workspace\Project1 but you can't rely on this.

  • thanks your reply. my .class file is currently in the following path D:\workspace\Project1\bin so shouldnt the output be as "D:\workspace\Project1\bin" – Tom Sep 17 '13 at 10:11
0

Your dir2 points to working directory (new File(".")). You can't get the location of your sources this way. Your file could sit inside the package (e.g. your.company.date.dateTesting). You should just manually concat the "src" to current working directory and then replace file package dots (.) with File.pathSeparator. In that way you will build the full path to your file.

Archer
  • 5,073
  • 8
  • 50
  • 96
0
String fullFilePath = "H:\\Shared\\Testing\\abcd.bmp";
    File file = new File(fullFilePath);

    String filePath = file.getAbsolutePath().substring(0,fullFilePath.lastIndexOf(File.separator));
    System.out.println(filePath);

Output: H:\Shared\Testing

MansoorShaikh
  • 913
  • 1
  • 6
  • 19
0

If you are doing this to try to read a file from the classpath, then check out this answer: How to really read text file from classpath in Java

Essentially you can do this

InputStream in = this.getClass().getClassLoader()
                            .getResourceAsStream("SomeTextFile.txt");

Otherwise, if you have some other requirement, one option is to pass through the src directory as a JVM arg when the application begins and then just read it back.

Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
0
/** The actual file running */
public static final File    JAR_FILE = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

/** The path to the main folder where the file .jar is run */
public static final String  BASE_DIRECTORY  = (JAR_FILE != null ? JAR_FILE.getAbsolutePath().replace(JAR_FILE.getName(), "") : "notFound");

This will work for you both in normal java execution and jar execution. This is the solution I am using in my project.

Gianmarco
  • 2,536
  • 25
  • 57
  • thanks for the reply. however this also does not include the source folder name. – Tom Sep 18 '13 at 04:52
  • check in different folders, because this should work and work for me, maybe you have some problem in your file position.... – Gianmarco Sep 18 '13 at 10:16
  • It is named Code Source, but really it is the path for bytecode source, thus, it is in the target folder. – Gangnus Feb 17 '20 at 15:37