unable to validate directory loaded from property file - java

I am trying to load a directory from a properties file. I have the following defined in the property file:
image.src.dir = "C:\\Temp\\foo\\"
Yes, the directory name is like that ... with mixed case. I have also tried simply referring to the directory as "/Temp/foo" with the same outcome.
I have the following code which fails despite the directory existing.
String srcDir = prop.getProperty("image.src.dir");
File folder = new File(srcDir);
if (!folder.isDirectory()) {
System.err.println("Directory: " + srcDir + " doesn't exist");
}
Thanks for the hint ...
The problem & solution:
solution: image.src.dir=C:\\Temp\\foo\\
problem: image.src.dir = "C:\\Temp\\foo\\"
That was my problem ..!

You have quotes in your property file. Quotes are needed for literal Strings in Java, but not Strings defined inside of a properties file.
Try this:
image.src.dir = C:\\Temp\\foo\\

Did you try to System.println(srcDir) if the string gets properly loaded from the properties file? Is the directory accessible (are the rights for superdirectories correct?).

Related

Access properties file in same jar

All our jars contain a certain file version.properties which contains specific information from the build.
When we start a jar from command line (with several jars on the class path), we would like access the version.properties from the same jar. To be more precise: We would like to write Java code that gives us the content of the properties file in the jar where the calling class file resides.
The problem is that all jars on the class path contain version.properties and we do not want to read the first on the class path but the one from the correct jar. How can we achieve that?
Funny problem. You have to find the location of a representative class of the particular jar and use the result to build the URL for the properties file. I hacked together an example using String.class as example and access MANIFEST.MF in META-INF, since the rt.jar has no properties in it (at least a quick jar tf rt.jar | grep properties resulted in zero results)
Class clazz = String.class;
String name = clazz.getName().replace('.', '/') + ".class";
System.out.println(name);
String loc = clazz.getClassLoader().getResource(name).toString();
System.out.println(loc);
if (loc.startsWith("jar:file")) {
String propertyResource = loc.substring(0, loc.indexOf('!')) + "!" + "/META-INF/MANIFEST.MF";
InputStream is = new URL(propertyResource).openStream();
System.out.println(propertyResource);
Properties props = new Properties();
props.load(is);
System.out.println(props.get("Implementation-Title"));
}

Java binary not picking up resources from the specified class path

I am trying to load a text file as InputStream, but the txt file is never picked up and the input stream value is always null. I don't know why this is happening and I would like to request assistance.
nohup java -jar crawler-jar-2014.11.0-SNAPSHOT.jar -classpath /home/nbsxlwa/crawler/resources/common-conf:/home/nbsxlwa/crawler/resources/dotcom-conf:./plugins/*:./lib/* &
The txt file is located in /home/nbsxlwa/crawler/resources/dotcom-conf directory. I can confirm that the file does exist, so I don't know why the file is not being picked up. The setup is given below:
`System.getEnvironment("java.class.path")
returns the following
value crawler-jar-2014.11.0-SNAPSHOT.jar
The code blocks are trying to create an input stream out of text.
String fileRules = conf.get(URLFILTER_REGEX_FILE);
System.out.println("file rules = " + fileRules);
// Pass the file as a resource in classpath.
// return conf.getConfResourceAsReader(fileRules);
// Pass the file as a resource in classpath.
InputStream is = RegexURLFilter.class.getResourceAsStream("/" + fileRules);
System.out.println("Inputstream is = " + is);
System.out.println(ClassLoader.getSystemResourceAsStream(fileRules));
The output to the above snippet is
file rules = regex-urlfilter.txt
Inputstream is = null
null
I tried adding classpath folders to the classpath MANIFEST.MF file. MANIFEST.MF contains the following entries, but the output still returned null.
Class-Path: resources/common-conf resources/dotcom-conf
resources/olb-conf lib/gora-cassandra-0.3.jar ** OTHER JARS**
Note the java man page entry for -jar
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
So your other classpath entries are ignored. You'll have to use the plain old java command, specifying a class with a main method and including your .jar in the classpath.
The relative paths you've specified in the MANIFEST are relative to the JAR itself or are complete URLs.

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());

How to specify a directory when creating a File object?

This should be a really simple question but Google + my code isn't working out.
In Eclipse on Windows, I want my program to look inside a certain folder. The folder is directly inside the Project folder, on the same level as .settings, bin, src, etc. My folder is called surveys, and that's the one I want my File object to point at.
I don't want to specify the full path because I want this to run on both of my computers. Just the path immediately inside my Project.
I'm trying this code but it isn't working - names[] is coming back null. And yes I have some folders and test junk inside surveys.
File file = new File("/surveys");
String[] names = file.list();
for(String name : names)
{
if (new File("/surveys/" + name).isDirectory())
{
System.out.println(name);
}
}
I'm sure my mistake is within the String I'm passing to File, but I'm not sure what's wrong?
In your question you didn't specify what platform you are running on. On non-Windows, a leading slash signifies an absolute path. Best to remove the leading slash. Try this:
File file = new File("surveys");
System.out.println("user.dir=" + System.getProperty("user.dir"));
System.out.println("file is at: " + file.getCanonicalPath());
String[] names = file.list();
for(String name : names)
{
if (new File(file, name).isDirectory())
{
System.out.println(name);
}
}
Make sure the in your run configuration, the program is running from the projects directory (user.dir = <projects>)
Make sure that your file is a directory before using file.list() on it, otherwise you will get a nasty NullPointerException.
File file = new File("surveys");
if (file.isDirectory()){
...
}
OR
if (names!=null){
...
}
If you checked the full path of your file with
System.out.println(file.getCanonicalPath())
the picture would immediately become clear. File.getCanonicalPath gives you exactly the full path. Note that File normalizes the path, eg on Windows "c:/file" is converted to "C:\file".

Why app_ appears while creating directory using ContextWraper.getDir

In my app, I used this code:
File DirectoryPath = cw.getDir("custom", Context.MODE_PRIVATE);
While creating a directory, and it returns:
/data/data/com.custom/app_custom**
So my question is why this app_ appears along with directory name. I know its default, but what actually it means?
And secondly, how can I create a sub-directory inside my directory i.e. app_custom in this case. if anyone knows please help me to understand this concept of getDir.
As far as I think, automatic "app_" added to user created data folders avoid any conflicts with system predefined application folders (folders inside application data folder i.e. cache, contents, databases etc. which are automatically created).
One method to create a sub folder inside those "app_..." folders, get absolute path of "app_..." folder, append required folder name to that and create using mkdirs()
e.g.
File dir = new File(newFolderPath);
dir.mkdirs()
Note: sub folders do not get "app_..." prefix
You can create a new Directory using the path that you are getting from getDir(),
File file = getDir("custom", MODE_PRIVATE);
String path = file.getAbsolutePath();
File create_dir = new File(path+"/dir_name");
if(!create_dir.exists()){
create_dir.mkdir();
}

Categories

Resources