Java: Why Absolute path missing 'src' folder - java

I'm passing relative path of My.class and when looking for 'dir.getAbsolutePath', it omits 'src' folder
String absuliteClassPath = "com/test/My.class"
File dir = new File(cls.substring(0, cls.lastIndexOf("/")));
System.out.println(dir.getAbsolutePath())
It prints
/Users/swapnil.kotwal/Swapnil/myproject/com/test
But is should print
/Users/swapnil.kotwal/Swapnil/myproject/src/com/test
Any Idea why ?

Related

how to change the work directory in java?

This is my project structure. I want to get the data folder's absolute path(/Users/user/IdeaProjects/project/data/post/sourceData) by using relative path, but the test I'm running is inside service folder, so the absolutePath is always shows "/Users/user/IdeaProjects/project/service/data/post/sourceData".
project
-data
-post
-sourceData
-service
-test
String relativePath = "data/post/sourceData";
Path absolutePath = Paths.get(relativePath).toAbsolutePath();
System.out.println(absolutePath);
I tried to set the working directory manually, but after I run the code it still print the error result, how can I fix this?
String newWorkingDirectory = "/Users/user/IdeaProjects/project/";
System.setProperty("user.dir", newWorkingDirectory);
String relativePath = "data/post/sourceData";
Path absolutePath = Paths.get(relativePath).toAbsolutePath();
System.out.println(absolutePath); // Outputs

How to get the path of the file from source root in java?

I have a file in java under the src folder, I want to get its path at runtime relative to the source folder.
For example-
myProject
-- src
-- packageOne
-- SomeFile.java
I would like to have the result packageOne/SomeFile.java. I couldn't find a way, I tried getPath(), getAbsoultePath() and every similar method.
You want to construct a relative path. AFAIK there is no ready function to use.
Given that you have one of the ancestor nodes (src) and you have SomeFile.java, the following code might work but I did not try...
File src = new File("...");
File somefile = new File("...");
String relpath = somefile.getName();
File cursor = somefile.getParentFile();
while (!cursor.getAbsolutePath().equals(src.getAbsolutePath()) {
somefile = cursor.getName() + File.pathSeparator + somefile;
}
System.out.println("relative path: "+relpath);

Getting relativePath and then diving deeper

I have a package structure like
A
|_B
|_C
|_D
|_"myFile.txt"
myMain.java
In my main i want to do something like
Path currentRelativePath = Paths.get("");
Which gives me the current relative path to where my Main() is located. Something along the lines of /home/myprojects/project1/src/ How do I amend the above to get me the relative path all the way down to the level of myFile.txt?
I have tried things like:
Path currentRelativePath = Paths.get("" + "/B/C/D/");
and
Path currentRelativePath = Paths.get("","B/C/D/");
and
Path currentRelativePath = Paths.get("");
Path finalPath = Paths.get(currentRelativePath.toString(),"/B/C/D/");
But in each case it only gets the "/B/C/D/" portion and not the beginning of the path.
//Gets from the current working directory a path to B/C/D
Path path = Paths.get("B/C/D");
//String equal to "B/C/D"
path.toString();
//String equal to "/home/myprojects/project1/src/B/C/D"
path.toAbsolutePath().toString();
I think you are confusing relative and absolute paths. /home/myprojects/project1/src/ is an absolute path, not as you say in your question "the current relative path"

Get directory from classpath (getting a file now)

I want to search for files in a directory. Therefore I want to get the directory in a File object but i'm getting a file instead of a directory. This is what I'm doing, it prints false but I want it to be true.
URL url = getClass().getResource("/strategy/viewconfigurations/");
File folder = new File(url.toString());
System.out.println(folder.isDirectory());
How can I load this way a directory?
It seems path or String you will got from the URL object cause problem.
You passed file path which you will got from the url.toString().
You need to change below line
File folder = new File(url.toString());
with this line
File folder = new File(url.getPath());
You need path of that folder which will you get from URL.getPath() function.
I hope this is what you need.
If you need an alternative for Java 7+ to Yagnesh Agola's post for finding a directory from a classpath folder, you could you also the newer java.nio.file.Path class.
Here is an example:
URL outputXml = Thread.currentThread().getContextClassLoader().getResource("outputXml");
if(outputXml == null) {
throw new RuntimeException("Cannot find path in classpath");
}
Path path = Paths.get(outputXml.toURI());

Getting the directory name in java

How do I get the directory name for a particular java.io.File on the drive in Java?
For example I have a file called test.java under a directory on my D drive.
I want to return the directory name for this file.
File file = new File("d:/test/test.java");
File parentDir = file.getParentFile(); // to get the parent dir
String parentDirName = file.getParent(); // to get the parent dir name
Remember, java.io.File represents directories as well as files.
With Java 7 there is yet another way of doing this:
Path path = Paths.get("d:/test/test.java");
Path parent = path.getParent();
//getFileName() returns file name for
//files and dir name for directories
String parentDirName = path.getFileName().toString();
I (slightly) prefer this way, because one is manipulating path rather than files, which imho better shows the intentions. You can read about the differences between File and Path in the Legacy File I/O Code tutorial
Note also that if you create a file this way (supposing "d:/test/" is current working directory):
File file = new File("test.java");
You might be surprised, that both getParentFile() and getParent() return null. Use these to get parent directory no matter how the File was created:
File parentDir = file.getAbsoluteFile().getParentFile();
String parentDirName = file.getAbsoluteFile().getParent();
File file = new File("d:/test/test.java");
String dirName = file.getParentFile().getName();
Say that you have a file called test.java in C:\\myfolder directory. Using the below code, you can find the directory where that file sits.
String fileDirectory = new File("C:\\myfolder\\test.java").getAbsolutePath();
fileDirectory = fileDirectory.substring(0,fileDirectory.lastIndexOf("\\"));
This code will give the output as C:\\myfolder

Categories

Resources