Question

Hi I have a java eclipse project which I want to execute from command line. I made a jar of it and am running it from command line.

I figured out how to access a file in a Jar by using getresourceasstream.

Ex:

InputStream is = Extractor.class.getResourceAsStream("CompanyNameListModified.txt");
BufferedReader  in=new BufferedReader(new InputStreamReader(is));`

What I wanna know now is how to access a directory from jar .

Currently:

Runtime.getRuntime().exec("hadoop fs -get /tmp/stockmarkets/ localfile");
File dir = new File("/home/hadoop/project/localfile");`

This gives a filenotfoundexception.

What I want to do is

File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
        ....
    }
}

Hence goto the directory and loop over each file in that directory.. How should I do it so it works for my JAR.?

So I tried this:

Runtime.getRuntime().exec("hadoop fs -get /tmp/stockmarkets/ localfile");
File dir =new File(Extractor.class.getResource("/home/hadoop/project/localfile").getPath());

error:

Exception in thread "main" java.lang.NullPointerException

I checked my directory it does have the local file directory.

Was it helpful?

Solution

You have to provide a filesystem path to use as a property

java -jar yourprogram.jar -DworkDir=/home/hadoop/project

or command-line argument, and then use that in both the call to hadoop and the File declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top