Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix linux pdf opening again #5945

Merged
merged 6 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +24,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
Expand All @@ -34,7 +37,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.
Expand All @@ -53,17 +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 p = Runtime.getRuntime().exec(cmdArray);

Process process = Runtime.getRuntime().exec(cmdArray);
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Down Expand Up @@ -118,7 +122,7 @@ public void openPdfWithParameters(String filePath, List<String> parameters) thro

openFileWithApplication(filePath, sj.toString());
} else {
openFile( filePath, "PDF");
openFile(filePath, "PDF");
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/jabref/gui/util/StreamGobbler.java
Original file line number Diff line number Diff line change
@@ -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<String> consumer;

public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}

@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer);
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
}
}
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved