0

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
sa_nyc
  • 971
  • 1
  • 13
  • 23
  • Any reason why you do want to use the [FileSystem API](http://hadoop.apache.org/docs/r2.3.0/api/org/apache/hadoop/fs/FileSystem.html) – Sudarshan May 07 '14 at 05:58
  • Because To access HDFS I will need to use HIVE/PIG which I want to avoid/. – sa_nyc May 07 '14 at 18:00
  • Accessing HDSFS does not need hive/pig you can do it via a normal standalone java program. Have a look [here](http://stackoverflow.com/questions/17564074/accessing-files-in-hdfs-using-java) – Sudarshan May 08 '14 at 05:05

1 Answers1

0

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • No I will know the directory destination beforehand. So I want to include in my java files the path to workDir. – sa_nyc May 07 '14 at 19:04
  • That's bad because you are then tied to a specific directory which may not exist in a different environment. – Jim Garrison May 07 '14 at 20:19
  • I am creating this directory as an output of another hadoop job which I will then access here. IT is a very job specific functionality which I need to implement – sa_nyc May 07 '14 at 22:34