I had a simple task to complete, unzip a file using java, because I wanted to it multiple times by reading a config file, the code was supposed to be very easy.
Something like this:
public static void deployZip(String ear, String sourceFile, String earBaseDir, XWPFTable table) { | |
try { | |
String pathToUnzip = earBaseDir + ear; | |
if (!Files.exists(Paths.get(pathToUnzip))) { | |
addRow(table, ear, sourceFile, "ear missing"); | |
return; | |
} | |
executeCommand("unzip " + sourceFile + pathToUnzip); | |
System.out.println("File " + sourceFile + " has been deployed to " + pathToUnzip); | |
addRow(table, ear, sourceFile, "success"); | |
} catch (Exception e) { | |
System.out.println("There was an exception while deploying " + sourceFile + " to " + ear + " : " + e.getMessage()); | |
addRow(table, ear, sourceFile, "Error"); | |
} | |
} | |
private static void executeCommand(String command) { | |
Process p; | |
try { | |
System.out.println("Shell command: " + command); | |
p = Runtime.getRuntime().exec(command); | |
p.waitFor(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
But it turns out that calling unzip hangs forever, so I found an stackoverflow post (https://stackoverflow.com/questions/34088099/executing-unzip-command-programmatically) that had the solution, and I ended doing it like this:
public static void deployZip(String ear, String sourceFile, String earBaseDir, XWPFTable table) { | |
try { | |
String pathToUnzip = earBaseDir + ear; | |
if (!Files.exists(Paths.get(pathToUnzip))) { | |
addRow(table, ear, sourceFile, "ear missing"); | |
return; | |
} | |
executeUnzip("/some/path/unzipFile.sh", pathToUnzip, sourceFile); | |
System.out.println("File " + sourceFile + " has been deployed to " + pathToUnzip); | |
addRow(table, ear, sourceFile, "success"); | |
} catch (Exception e) { | |
System.out.println("There was an exception while deploying " + sourceFile + " to " + ear + " : " + e.getMessage()); | |
addRow(table, ear, sourceFile, "Error"); | |
} | |
} | |
private static void executeUnzip(String command, String location, String zipFile) { | |
Process p; | |
StringBuffer output = new StringBuffer(); | |
try { | |
System.out.println("Shell command: " + command); | |
p = Runtime.getRuntime().exec(new String[]{command, location, zipFile}); | |
p.waitFor(); | |
// This reader is just to disaply the output of the command executed. | |
BufferedReader reader = | |
new BufferedReader(new InputStreamReader(p.getInputStream())); | |
String line = ""; | |
while ((line = reader.readLine()) != null) { | |
output.append(line + "\n"); | |
} | |
System.out.println(output.toString()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
As you can see this is a little bit different, it calls unzipFile.sh that basically does execute unzip command with the parameters passed by.
I was not alone with this problem, there are plenty of posts and questions about this on stackoverflow, but for some reason these did not work for me:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
Comments