Get parent directory's parent directory name in java - java

I have current working file's directory path I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\WEB-INF\classes\PackageName\
I want path like I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\. How can I do this in java code?

You better get your App-Path from the ServletContext in Servlet-Containern like Tomcat.
Use
servletContext.getRealPath(".");
// will return 'I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\' on win.
// will return '/usr/local/tomcat/webapps/ExecutableFileProcess' on unix.

Have a look at
http://developer.android.com/reference/java/io/File.html#getParentFile()
Applying this twice will probably get you the right directory.
But note that you program might or might not be allowed to go there, based on the settings in the file system and the context your code runs in.

Try this:
Path aPath = Paths.get("I:\\apache-tomcat-7.0.40\\webapps\\ExecutableFileProcess\\WEB-INF\\classes\PackageName\\");
Path parentsParentpath = aPath.getParent().getParent();

Related

Error message: The file "countries.geo.json" is missing or inaccessible

I am taking the Coursera OOP in Java class. In the module 4 assignment, I run the code that the course provides in EarthquakeCityMap.java,
and I get an error as "The file "countries.geo.json" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
Exception in thread "Animation Thread" java.lang.NullPointerException"
I tried to set countryFile as
"../data/countries.geo.json",
"data/countries.geo.json",
and the complete path of countries file,
but still didn't solve the problem.
//this error points to the code
private String countryFile = "countries.geo.json";
List<Feature> countries = GeoJSONReader.loadData(this, countryFile);"
//the countries file is saved in data folder.
Poject folder listing
"countries.geo.json" (unless changed in GeoJSONReader manipulates this path) will be relative to the compiled java .class files in the IntelliJ's project out folder.
If this in GeoJSONReader.loadData(this, countryFile); is a PApplet instance you can use sketchPath() to make that path relative to the folder from which the sketch runs:
List<Feature> countries = GeoJSONReader.loadData(this, this.sketchPath("data"+File.separator+countryFile));
The above snippet is based on an assumption so the syntax in your code might be slightly different, but hopefully this illustrates how you'd use sketchPath().
Additionally there's a dataPath() as well which you can test from your main PApplet in setup() as a test:
String fullJSONPath = dataPath("countries.geo.json");
println("fullJSONPath: " + fullJSONPath);//hopefully this prints the full path to the json file on your machine
println(new File(fullJSONPath).exists());//hopefully this prints true
If you specified the full path and it didn’t work, you probably forgot to escape the \ character with another backslash. The backslash character is special and needs to be doubled for windows path to be interpreted properly. For instance “c:\\users\\...”. You can also specify / instead of \ and it would work : “c:/users/...”
That said, the path resolution of a file when relative (IE not being absolute to the file system root) is relative to the working directory of the executed app. Typically, in an IDE without any special configuration, the working directory would be the root path of the project. So in order to get the relative file path resolved properly, you would have to specify the path as “data/countries.geo.json”.
You can also find out what path you are in when you run the app by doing a System.out.println(new java.io.File(“.”).getAbsolutePath()) and craft the relative path according to this folder.

Java nio: How to add extension to an absolute path?

This feels like it should be something straight forward, but I can seem to find an elegant solution to it without converting to File.
Given a Path
Path path = Paths.get("/a/b/foo")
How to do get the path /a/b/foo.bar? subpath will return a relative path regardless of whether the original path is relative or absolute.
I would prefer not to have to use additional libraries. But, maybe that is the only way?
To change the file name of a Path, use one of the resolveSibling() methods:
This is useful where a file name needs to be replaced with another file name.
Using this method ensures that the result Path object is for the same FileSystem as the source Path object.
So, to add extension ".bar" to a Path:
path = path.resolveSibling(path.getFileName() + ".bar");

Get resource(jar) path in java

I have a class XYZ in application that is executed as jar in any directory. I want to find the current directory path in which jar is executing for this I have used following code.
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
I write this code in public constructor of XYZ class but it is not working though I am using it since long time it works fine, but now it is not returning the current directory path.
please mark and suggest what is going wrong in this.
When you execute your statement within "this" - it means that you are trying to locate current instance of the class but you need to find the class itself.
In your case you can use this:
String path = XYZ.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
It will give you the path you are looking for and will solve issues with whitespaces and special characters inside this path.
In case you are doing this for Linux it might be useful to use:
URLDecoder.decode(ClassLoader.getSystemClassLoader().getResource(".").getPath(‌​), "UTF-8");
...but now it is not returning the current directory path.
It shouldn't return the current directory path. It should return the path of the jar file the class is coming from, or the root folder where the class files are loaded from (if not packed in a jar). It might and it might not be the current folder.
Calling
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
will try to locate the path the class of the current instance (this) is coming from.
Make sure that the Class you start from is part of the jar file. E.g. do not use this but use an explicit class which you're sure is from the jar file, e.g.
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()
Also note that this won't work if you start the application from your IDE in which case the result would be the bin folder (the root folder of compiled class files).
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
OR
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().toURI());

How to get the path to the java file with the main method (src dir) [duplicate]

I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass().
getClassLoader().getResource("unibo.lsb.res/dice.jpg");
The directory structure is the following:
unibo/
lsb/
res/
dice.jpg
test/
..../ /* other packages */
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue.
Any hint?
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
leading slash to denote the root of the classpath
slashes instead of dots in the path
you can call getResource() directly on the class.
Instead of explicitly writing the class name you could use
this.getClass().getResource("/unibo/lsb/res/dice.jpg");
if you are calling from static method, use :
TestGameTable.class.getClassLoader().getResource("dice.jpg");
One thing to keep in mind is that the relevant path here is the path relative to the file system location of your class... in your case TestGameTable.class. It is not related to the location of the TestGameTable.java file.
I left a more detailed answer here... where is resource actually located

Get a resource using getResource()

I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass().
getClassLoader().getResource("unibo.lsb.res/dice.jpg");
The directory structure is the following:
unibo/
lsb/
res/
dice.jpg
test/
..../ /* other packages */
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue.
Any hint?
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
leading slash to denote the root of the classpath
slashes instead of dots in the path
you can call getResource() directly on the class.
Instead of explicitly writing the class name you could use
this.getClass().getResource("/unibo/lsb/res/dice.jpg");
if you are calling from static method, use :
TestGameTable.class.getClassLoader().getResource("dice.jpg");
One thing to keep in mind is that the relevant path here is the path relative to the file system location of your class... in your case TestGameTable.class. It is not related to the location of the TestGameTable.java file.
I left a more detailed answer here... where is resource actually located

Categories