Elsban
Elsban

Reputation: 1297

How is this file being passed in Java?

I run this program which has this run command:

gzip -dc file.gz | java driver

Can someone explain how this works (how is this file.gz being passed to the java program) and how can I do this using Netbeans.

Regards!

Upvotes: 0

Views: 33

Answers (1)

11thdimension
11thdimension

Reputation: 10633

A pipe | in linux is a convenience for redirecting output of a program to input of another.

For example suppose you're trying to locate a file with name java in current directory. Then ls -l will list the file but to do the second part we can just pipe this output to grep like below.

ls -l | grep java

If it were not so, then we would have to first save the output of ls -l into a file then use that file with grep to search.

Same is happening in your example gzip -dc.

-d stands for decompress
-c output to the console (not a file)

Now since gzip output (binary output) is dumped on the console we can use pipe to feed to Java program.

In java driver driver is an executable JAR file (without extension .jar in your case). driver JAR has a main method which receives command line arguments.

Now driver's main method is expecting the binary content to it's command line input parameter (main(String[] args), where args is array of command line parameters).

If you want to use this utility for your project then you have to somehow decompress the gzip file using Java and then store the output in a String and pass it to the driver's main method.

String gzipStr = ... extract gzip as string
com.example.DriverMain.main({gzipStr});//suppose that main method is in com.example.DriverMain class

You can find the main class by looking at the META-INF/MANIFEST.MF content inside the driver JAR.

Upvotes: 1

Related Questions