From c430b0e72ce5843313373275335ac00c521acc9e Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Thu, 13 Feb 2020 20:46:20 +0100 Subject: [PATCH 1/4] Try to fix linux pdf opening again --- .../java/org/jabref/gui/desktop/os/Linux.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index a39c6e335d2..4c11217b5f9 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -22,6 +22,7 @@ import static org.jabref.preferences.JabRefPreferences.USE_PDF_READER; public class Linux implements NativeDesktop { + private static final Logger LOGGER = LoggerFactory.getLogger(Linux.class); @Override @@ -34,7 +35,7 @@ public void openFile(String filePath, String fileType) throws IOException { } else { viewer = "xdg-open"; } - String[] cmdArray = { viewer, filePath }; + String[] cmdArray = {viewer, filePath}; Process p = Runtime.getRuntime().exec(cmdArray); // When the stream is full at some point, then blocks the execution of the program // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. @@ -57,13 +58,21 @@ public void openFileWithApplication(String filePath, String application) throws String[] cmdArray = new String[openWith.length + 1]; System.arraycopy(openWith, 0, cmdArray, 0, openWith.length); cmdArray[cmdArray.length - 1] = filePath; - Process p = Runtime.getRuntime().exec(cmdArray); + Process process = Runtime.getRuntime().exec(cmdArray); // When the stream is full at some point, then blocks the execution of the program // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. - BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line; - line = in.readLine(); - LOGGER.debug("Received output: " + line); + while ((line = in.readLine()) != null) { + LOGGER.debug("Received output from error stream: " + line); + } + + BufferedReader standardIn = new BufferedReader(new InputStreamReader(process.getInputStream())); + String outputLine; + while ((outputLine = standardIn.readLine()) != null) { + LOGGER.debug("Received output: " + outputLine); + } + } @Override @@ -118,7 +127,7 @@ public void openPdfWithParameters(String filePath, List parameters) thro openFileWithApplication(filePath, sj.toString()); } else { - openFile( filePath, "PDF"); + openFile(filePath, "PDF"); } } From 2dad2221bee92b0a31f57d706be5b1081c27bef7 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Fri, 14 Feb 2020 20:23:10 +0100 Subject: [PATCH 2/4] execute stream gobbler in own task --- .../java/org/jabref/gui/desktop/os/Linux.java | 19 ++++++---------- .../org/jabref/gui/util/StreamGobbler.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/jabref/gui/util/StreamGobbler.java diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index 4c11217b5f9..f8e3e48fa8d 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -11,8 +11,10 @@ import java.util.Optional; import java.util.StringJoiner; +import org.jabref.JabRefExecutorService; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.gui.util.StreamGobbler; import org.jabref.preferences.JabRefPreferences; import org.slf4j.Logger; @@ -54,25 +56,18 @@ public void openFileWithApplication(String filePath, String application) throws } else { openWith = new String[] {"xdg-open"}; } - String[] cmdArray = new String[openWith.length + 1]; System.arraycopy(openWith, 0, cmdArray, 0, openWith.length); cmdArray[cmdArray.length - 1] = filePath; + Process process = Runtime.getRuntime().exec(cmdArray); // When the stream is full at some point, then blocks the execution of the program // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. - BufferedReader in = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String line; - while ((line = in.readLine()) != null) { - LOGGER.debug("Received output from error stream: " + line); - } - - BufferedReader standardIn = new BufferedReader(new InputStreamReader(process.getInputStream())); - String outputLine; - while ((outputLine = standardIn.readLine()) != null) { - LOGGER.debug("Received output: " + outputLine); - } + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); + StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); + JabRefExecutorService.INSTANCE.execute(streamGobblerInput); + JabRefExecutorService.INSTANCE.execute(streamGobblerError); } @Override diff --git a/src/main/java/org/jabref/gui/util/StreamGobbler.java b/src/main/java/org/jabref/gui/util/StreamGobbler.java new file mode 100644 index 00000000000..d0850df09e9 --- /dev/null +++ b/src/main/java/org/jabref/gui/util/StreamGobbler.java @@ -0,0 +1,22 @@ +package org.jabref.gui.util; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.function.Consumer; + +public class StreamGobbler implements Runnable { + + private InputStream inputStream; + private Consumer consumer; + + public StreamGobbler(InputStream inputStream, Consumer consumer) { + this.inputStream = inputStream; + this.consumer = consumer; + } + + @Override + public void run() { + new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer); + } +} \ No newline at end of file From fa516acde2068a10fede07b28e657593407776c0 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 15 Feb 2020 00:41:20 +0100 Subject: [PATCH 3/4] add to other open method as well --- src/main/java/org/jabref/gui/desktop/os/Linux.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index f8e3e48fa8d..dfe614f2523 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -38,13 +38,14 @@ public void openFile(String filePath, String fileType) throws IOException { viewer = "xdg-open"; } String[] cmdArray = {viewer, filePath}; - Process p = Runtime.getRuntime().exec(cmdArray); + Process process = Runtime.getRuntime().exec(cmdArray); // When the stream is full at some point, then blocks the execution of the program // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. - BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream())); - String line; - line = in.readLine(); - LOGGER.debug("Received output: " + line); + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); + StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); + + JabRefExecutorService.INSTANCE.execute(streamGobblerInput); + JabRefExecutorService.INSTANCE.execute(streamGobblerError); } @Override From 032b063ae315bc71b981e592eb0069281f464962 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Wed, 19 Feb 2020 19:30:32 +0100 Subject: [PATCH 4/4] use process builder and close reader --- src/main/java/org/jabref/gui/desktop/os/Linux.java | 12 +++++------- .../java/org/jabref/gui/util/StreamGobbler.java | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index dfe614f2523..579b4986774 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -37,10 +37,8 @@ public void openFile(String filePath, String fileType) throws IOException { } else { viewer = "xdg-open"; } - String[] cmdArray = {viewer, filePath}; - Process process = Runtime.getRuntime().exec(cmdArray); - // When the stream is full at some point, then blocks the execution of the program - // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. + ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath); + Process process = processBuilder.start(); StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); @@ -61,9 +59,9 @@ public void openFileWithApplication(String filePath, String application) throws System.arraycopy(openWith, 0, cmdArray, 0, openWith.length); cmdArray[cmdArray.length - 1] = filePath; - Process process = Runtime.getRuntime().exec(cmdArray); - // When the stream is full at some point, then blocks the execution of the program - // See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process. + ProcessBuilder processBuilder = new ProcessBuilder(cmdArray); + Process process = processBuilder.start(); + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); diff --git a/src/main/java/org/jabref/gui/util/StreamGobbler.java b/src/main/java/org/jabref/gui/util/StreamGobbler.java index d0850df09e9..b2131968f87 100644 --- a/src/main/java/org/jabref/gui/util/StreamGobbler.java +++ b/src/main/java/org/jabref/gui/util/StreamGobbler.java @@ -1,12 +1,18 @@ package org.jabref.gui.util; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.function.Consumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class StreamGobbler implements Runnable { + private static final Logger LOGGER = LoggerFactory.getLogger(StreamGobbler.class); + private InputStream inputStream; private Consumer consumer; @@ -17,6 +23,10 @@ public StreamGobbler(InputStream inputStream, Consumer consumer) { @Override public void run() { - new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + reader.lines().forEach(consumer); + } catch (IOException e) { + LOGGER.error("Error when reading process stream from external application", e); + } } -} \ No newline at end of file +}