Thursday, 15 August 2013

Java Class Read a Yaml Inside Jar

Java Class Read a Yaml Inside Jar

I am trying to read a file inside my jar from another class inside the
jar. However I am continually getting the same error: Caught: class
java.io.FileNotFoundException while attempting to read metrics:
metrics.yml
At first I had my code do something like this, assuming it was from the
path of the class:
String yamlPath = ".." + File.separator + ".." + File.separator + ".." +
File.separator + ".." + File.separator + "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I also did this assuming it might take the path from the base of the jar:
String yamlPath = "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I then noticed this thread How to a read file from jar in Java? and found
out that I needed a "/" before my path. I tried both methods above using
the slash as well.
String yamlPath = File.seperator + ".." + File.separator + ".." +
File.separator + ".." + File.separator + ".." + File.separator +
"myYaml.yml";
OR
String yamlPath = File.seperator + "myYaml.yml";
I am completely lost on what to do about this error now. Is there
something I am not getting about the jar structure? Why can my file not be
found. Thanks in advance for any help / information.
Sorry, I forgot to mention where it is in the JAR: The class is in the
following path: com/a/b/c/myclass.class The yaml is in the following path:
myYaml.yml

No comments:

Post a Comment