-1

I am trying to access some statistics (file type description) of files by executing the 'file' command via java. Current implementation using Runtime.exec() and it's a bit overhead for my application

Is there any pure Java wrapper for Unix file command?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
davaj22251
  • 19
  • 4

3 Answers3

0

You can try Files.probeContentType

rkosegi
  • 14,165
  • 5
  • 50
  • 83
0

Since java is platform-independent, no it doesn't have any Unix or other OS-specific wrappers. Runtime.exec() is an acceptable way to do what you want, but better yet to use ProcessBuilder class (See method start()). Alternatively, you could access directories and files using regular java classes. See class File or read about package java.nio.file

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

jproc is a library that makes calling external tools straight-forward. It handles the consumption of stdin and stdout and also checks the exit code.

Calling the file utility becomes a one liner:

String output = ProcBuilder.run("file", "somepath");

The only thing that is left to do is parsing the output.

To get it onto your classpath you can use its maven coordinate:

<dependency>
    <groupId>org.buildobjects</groupId>
    <artifactId>jproc</artifactId>
    <version>2.2.3</version>
</dependency>
Felix Leipold
  • 1,064
  • 10
  • 17