Run java -jar from exec - java

I'm try to run:
java -jar /home/user/workspace/maltparser-1.8/maltparser-1.8.jar
This is work. And return:
-----------------------------------------------------------------------------
MaltParser 1.8
-----------------------------------------------------------------------------
MALT (Models and Algorithms for Language Technology) Group
Vaxjo University and Uppsala University
Sweden
-----------------------------------------------------------------------------
Usage: java -jar maltparser-1.8.jar -f <path to option file> <options> java -jar maltparser-1.8.jar -h for more help and options
help ( -h) : Show options
----------------------------------------------------------------------------- option_file ( -f) : Path to option file
----------------------------------------------------------------------------- verbosity *( -v) : Verbosity level debug - Logging of debugging messages error - Logging of error events fatal - Logging of very severe error events info
- Logging of informational messages off - Logging turned off warn - Logging of harmful situations
-----------------------------------------------------------------------------
Documentation: docs/index.html
And now, I'm try to run this .jar from my class:
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
Process ps = Runtime.getRuntime().exec(new String[]{"java","-jar","/home/user/workspace/maltparser-1.8/maltparser-1.8.jar"});
ps.waitFor();
java.io.InputStream is=ps.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);
System.out.println(new String(b));
}
}
And it return me nothing... I want to intercept output stream. How I can do it?

IMHO, the your method of reading the InputStream is a little weird, you probably don't want to wait till the stream is fill before printing something out, also, you ignoring the error stream...
I prefer to use ProcessBuilder because...
You can redirect the error stream to the InputStream, which makes it easier to manage and
You can change the context of the working directory from which the command will be started in...
As an example...
try {
ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "Your.jar"});
pb.redirectError();
//pb.directory(new File("you/path"));
Process ps = pb.start();
try (java.io.InputStream is = ps.getInputStream()) {
int read = -1;
while ((read = is.read()) != -1) {
System.out.print((char) read);
}
}
System.out.println("Command exited with: " + ps.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
Updated
For reasons that I'm unaware of, output sent through log4j's ConsoleAppender doesn't seem to reach the Processs InputStream...
To resolve this, you can use ProcessBuilders inheritIO...
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class TestRunJar {
public static void main(String[] args) {
try {
ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "/../maltparser-1.8.jar"});
pb.inheritIO();
pb.redirectError();
pb.directory(new File("/..")); // Path to where maltparser-1.8.jar resides
Process ps = pb.start();
InputStreamConsumer stdout = new InputStreamConsumer(ps.getInputStream());
InputStreamConsumer stderr = new InputStreamConsumer(ps.getErrorStream());
stderr.start();
stdout.start();
stderr.join();
stderr.join();
System.out.println("Command exited with: " + ps.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
}
public static class InputStreamConsumer extends Thread {
private InputStream is;
private IOException exp;
private StringBuilder output;
public InputStreamConsumer(InputStream is) {
this.is = is;
}
#Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}
public StringBuilder getOutput() {
return output;
}
public IOException getException() {
return exp;
}
}
}
This is just my test code, so it might be a little over the top ;)
Which finally dumped out...
-----------------------------------------------------------------------------
MaltParser 1.8
-----------------------------------------------------------------------------
MALT (Models and Algorithms for Language Technology) Group
Vaxjo University and Uppsala University
Sweden
-----------------------------------------------------------------------------
Usage:
java -jar maltparser-1.8.jar -f <path to option file> <options>
java -jar maltparser-1.8.jar -h for more help and options
help ( -h) : Show options
-----------------------------------------------------------------------------
option_file ( -f) : Path to option file
-----------------------------------------------------------------------------
verbosity *( -v) : Verbosity level
debug - Logging of debugging messages
error - Logging of error events
fatal - Logging of very severe error events
info - Logging of informational messages
off - Logging turned off
warn - Logging of harmful situations
-----------------------------------------------------------------------------
Documentation: docs/index.html
Command exited with: 0

Related

PHP - Serial Port (com port) access denied in WAMP server (Windows)

I have been trying to write a PHP script to send SMS messages via a GSM modem connected to the server machine.
Below is the aforesaid PHP script.
<?php
$device = "COM7";
exec("mode $device BAUD=9600 PARITY=n DATA=8");
$comport = fopen($device, "w");
fputs($comport,"AT+CMGF=1\n\r");
fputs($comport,"AT+cmgs=\"xxxxxxxxxx\"\n\r");
fputs($comport,"sms text\n\r");
fputs($comport,chr(26));
fclose($comport);
echo "done";
?>
When I try to run the above code I get the following error;
Warning: fopen(COM7): failed to open stream: Permission denied in
E:\wamp\www\sms\index.php on line 4
and
Warning: fclose() expects parameter 1 to be resource, boolean given in
E:\wamp\www\sms\index.php on line 9
I have tried changing the user permission in windows to allow WAMP server full access, which WAMP server already had by default.
As a workaround I have tried using the DIO extension which gave a similar error when trying to open a port. DIO also gave an permission denied error.
It was suggested here in stackoverflow to use the physical address of the GSM modem instead of "COM7", I tried it didn't work. Another answer had suggested to add the www-data to the dialout group, but that solution was for Linux.
I tried writing a Java servlet as a workaround for this issue. The Java program runs as a stand alone file, but when its run as a servlet via Tomcat, it again gives an "Access Violation" error. I used RXTX library for the Java servlet.
So I am guessing this error is caused by lack of permission assigned to whoever is trying to access the com port via the server. (I get "SYSTEM" when I ran echo get_current_user(); from the above PHP script)
I am trying to find the root cause of this error. Your help is much appreciated.
Below is the Java SMS sender class which uses the RXTX Library.
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SMSsender {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString1 = "AT";
static String messageString3 = "AT+CMGF=1";
static String messageString4 = "AT+CMGS=\"+xxxxxxxxxx\"";
static String messageString5 = "TESTY2";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static char enter = 13;
static char CTRLZ = 26;
public void sendMessage(String[] args) throws InterruptedException {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM7")) {
try {
serialPort = (SerialPort) portId.open("COM7", 2000);
} catch (PortInUseException e) {
System.out.println("err");
}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
try {
outputStream.write((messageString1 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
// outputStream.write((messageString2 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString3 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString4 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString5 + CTRLZ).getBytes());
outputStream.flush();
Thread.sleep(100);
System.out.println("step 1");
Thread.sleep(3000);
outputStream.close();
serialPort.close();
System.out.println("step 2");
} catch (IOException e) {
e.printStackTrace();
serialPort.close();
} finally {
serialPort.close();
}
}
}
}
}
}
Above code works when its run outside of the server as a stand alone application.
Following is the servlet code which invokes the above class to send an SMS.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class sendSMS extends HttpServlet {
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Set the response MIME type of the response message
response.setContentType("text/html");
// Allocate a output writer to write the response message into the network socket
PrintWriter out = response.getWriter();
SMSsender obj = new SMSsender();
try {
obj.sendMessage(null);
}
catch(Exception e)
{
out.println(e.getMessage());
}
finally {
out.close(); // Always close the output writer
}
}
}
When this servlet is run through Apache Tomcat, the server automatically closes with the following error in the log.
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000001cf32174465, pid=5828, tid=2276
#
# JRE version: Java(TM) SE Runtime Environment (10.0.2+13) (build 10.0.2+13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (10.0.2+13, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)
# Problematic frame:
# C [rxtxSerial.dll+0x4465]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
I was able to solve the issue.
Apparently, windows no longer allows to communicate with serial ports via virtual file names. In the case of PHP, I was trying to get in-touch with the com port via fopen("com7"). So this method is no longer viable it seems. This is according to David Gibson. View Source Here . I would love to read more on this if I could find the original source though.
According to David Gibson's post, PHP extensions like DIO is not very stable in the Windows environment. So I stopped trying to find a solution to my PHP code.
Now to the Java workaround
Earlier I was trying to use the RXTX java library. Although the code worked as a standalone application, it failed with an "Access Violation" error when run via tomcat.
I wonder if RXTX is still in active development.
Anyhow I tired out the platform independent solution jSerialComm. Pretty amazing, it doesn't require any DLLs or any other native libraries.
I was finally able to send a SMS via a servlet thanks to this awesome, easy to use library. I have posted my working Java code below for your reference.
I hope this will help someone who is facing the same problem! Cheers!
And thanks to Mehdi, Erwin and Sanguinary for the guidance given with regards to using this amazing Stackoverflow platform and of course to the developers!
Note: This code below is messy and not optimised. This code is for reference only.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.io.InputStream;
import java.util.Scanner;
import com.fazecast.jSerialComm.*;
public class sendSMS extends HttpServlet {
SerialPort ubxPort;
static String messageString1 = "AT";
static String messageString2 = "AT+CMGF=1";
static String messageString3 = "AT+CMGS=\"+xxxxxxxxxx\"";
static String messageString4 = "TESTY2";
static char enter = 13;
static char CTRLZ = 26;
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the response MIME type of the response message
response.setContentType("text/html");
// Allocate a output writer to write the response message into the network socket
PrintWriter out = response.getWriter();
try {
ubxPort = SerialPort.getCommPort("com7");
boolean openedSuccessfully = ubxPort.openPort();
out.println("h1<br>");
if(openedSuccessfully)
{
out.println("Port Opened<br>");
byte[] buffer = (messageString1 + enter).getBytes();
ubxPort.writeBytes(buffer, buffer.length);
out.println("1 sent<br>");
InputStream in = ubxPort.getInputStream();
try
{
for (int j = 0; j < 1000; ++j)
System.out.print((char)in.read());
in.close();
} catch (Exception e) { e.printStackTrace(); }
buffer = (messageString2 + enter).getBytes();
ubxPort.writeBytes(buffer, buffer.length);
out.println("2 sent<br>");
try
{
for (int j = 0; j < 1000; ++j)
System.out.print((char)in.read());
in.close();
} catch (Exception e) { e.printStackTrace(); }
buffer = (messageString3 + enter).getBytes();
ubxPort.writeBytes(buffer, buffer.length);
out.println("3 sent<br>");
try
{
for (int j = 0; j < 1000; ++j)
System.out.print((char)in.read());
in.close();
} catch (Exception e) { e.printStackTrace(); }
buffer = (messageString4 + CTRLZ).getBytes();
ubxPort.writeBytes(buffer, buffer.length);
out.println("4 sent<br>");
try
{
for (int j = 0; j < 1000; ++j)
System.out.print((char)in.read());
in.close();
} catch (Exception e) { e.printStackTrace(); }
}
}
catch(Exception e)
{
out.println("here3");
out.println(e.getMessage());
}
finally {
ubxPort.closePort();
out.println("here4");
out.close(); // Always close the output writer
}
}
}

Java Runtime not capturing STDOUT on Mac

Mac OS here, but looking for a solution that is platform agnostic. Also please note, even though Consul is mentioned here, it is just arbitrary and the solution should have nothing to do with, nor require knowledge of, Consul.
When I open a shell and run consul -v (to determine if Consul is installed locally), I get the following STDOUT:
Consul v0.5.2
Consul Protocol: 2 (Understands back to: 1)
When I run the following code:
public class VerifyConsul {
public static void main(String[] args) {
PrintStream oldPS = System.out;
try {
Runtime runtime = Runtime.getRuntime();
Process proc;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newPS = new PrintStream(baos);
System.setOut(newPS);
proc = runtime.exec(“consul -v”);
proc.waitFor();
String capturedOut = baos.toString();
if(capturedOut.isEmpty()) {
throw new IllegalArgumentException(“Consul not found.”);
}
} catch(Throwable t) {
System.out.println(t.getMessage());
System.setOut(oldPS);
}
}
}
I get the IllegalArgumentException stating that Consul [is] not found.
What is wrong with my code? Why isn’t it “hooking”/capturing STDOUT?
Use Process#getInputStream() to read STDOUT or Process#getErrorStream() to read STDERR
Here's an example (using java process and reading STDERR):
package so32589604;
import org.apache.commons.io.IOUtils;
public class App {
public static void main(String[] args) throws Exception {
final Runtime runtime = Runtime.getRuntime();
final Process proc = runtime.exec("java -version");
proc.waitFor();
// IOUtils from apache commons-io
final String capturedOut = IOUtils.toString(proc.getErrorStream());
System.out.println("output = " + capturedOut);
if(capturedOut.isEmpty()) {
throw new IllegalArgumentException("Java not found.");
}
}
}

Run Non GUI Jar Files

I know that I can run non GUI jar files from the command line. Is there any way that can do so by clicking or something and not writing the commands again and again.? Is there any software to do so. ( I am talking about a compiled jar and don't want to run from any ide)
public static final String TITLE = "CONSOLE title";
public static final String FILENAME = "myjar.jar";
public static void main(String[] args) throws InterruptedException, IOException {
if(args.length==0 || !args[args.length-1].equals("terminal")) {
String[] command;
if(System.getProperty("os.name").toLowerCase().contains("win")) {
command = new String[]{"cmd", "/c", "start \"title \\\""+TITLE+"\\\" & java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
} else {
command =new String[]{"sh", "-c", "gnome-terminal -t \""+TITLE+"\" -x sh -c \"java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
}
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch(Throwable t){
t.printStackTrace();
}
} else {
//THERE IS YOUR CONSOLE PROGRAM:
System.out.println("Hey! What's your name?");
String read = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Hey, "+read+"!");
Thread.sleep(10000);
}
}
You can run it with double clicking on .jar file. Don't forget about MANIFEST.MF! :) (working on linux, also!)
Example (I only double clicked on jar file):
The way intended by Java is that you call java -jar XXXX.jar on the jars you need. Drawback is that you can't specify a classpath so all classes should be there.
A cooler way to package an application is by using Java WebStart. With that the user installs the application jut by clicking on a web browser. Check here http://docs.oracle.com/javase/8/docs/technotes/guides/javaws/developersguide/contents.html

Running java program from another java program

I'm trying to run a Java program from another Java application. Here is my code:
public class Main {
public static int Exec() throws IOException {
Process p = Runtime.getRuntime().exec("javac -d C:/Users/Dinara/Desktop/D/bin "
+ "C:/Users/Dinara/Desktop/D/src/test.java");
Process p1 = Runtime.getRuntime().exec("java -classpath C:/Users/Dinara/Desktop/D/bin test");
return 0;
}
public static void main(String[] args) throws IOException {
Exec();
}
}
javac works fine and creates test.class file in bin directory. However java -classpath C:/Users/Dinara/Desktop/D/bin test does not run the test.class file.
the content of the test.java:
import java.io.*;
class test {
public static void main(String args[]) {
try {
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
I suppose that something wrong with recognizing Java command. Could you please give me a sample code for fixing this problem or share idea? I'm using Netbeans to run Main class and the location of the application folder is C:\Users\Dinara\Main
Use
System.getProperty("java.home") + "/bin/java -classpath C:/Users/Dinara/Desktop/D/bin test"
instead of
"java -classpath C:/Users/Dinara/Desktop/D/bin test"
You need to supply the full path to the javac, exec won't use the ath to find it for you

How to open the default webbrowser using java

Can someone point me in the right direction on how to open the default web browser and set the page to "www.example.com" thanks
java.awt.Desktop is the class you're looking for.
import java.awt.Desktop;
import java.net.URI;
// ...
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("http://www.example.com"));
}
For me solution with Desktop.isDesktopSupported() doesn't work (windows 7 and ubuntu). Please try this to open browser from java code:
Windows:
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
Mac
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
rt.exec("open " + url);
Linux:
Runtime rt = Runtime.getRuntime();
String url = "http://stackoverflow.com";
String[] browsers = { "google-chrome", "firefox", "mozilla", "epiphany", "konqueror",
"netscape", "opera", "links", "lynx" };
StringBuffer cmd = new StringBuffer();
for (int i = 0; i < browsers.length; i++)
if(i == 0)
cmd.append(String.format( "%s \"%s\"", browsers[i], url));
else
cmd.append(String.format(" || %s \"%s\"", browsers[i], url));
// If the first didn't work, try the next browser and so on
rt.exec(new String[] { "sh", "-c", cmd.toString() });
If you want to have multiplatform application, you need to add operation system checking(for example):
String os = System.getProperty("os.name").toLowerCase();
Windows:
os.indexOf("win") >= 0
Mac:
os.indexOf("mac") >= 0
Linux:
os.indexOf("nix") >=0 || os.indexOf("nux") >=0
Here is my code. It'll open given url in default browser (cross platform solution).
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Browser {
public static void main(String[] args) {
String url = "http://www.google.com";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
As noted in the answer provided by Tim Cooper, java.awt.Desktop has provided this capability since Java version 6 (1.6), but with the following caveat:
Use the isDesktopSupported() method to determine whether the Desktop API is available. On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false.
For platforms which do not support or provide java.awt.Desktop, look into the BrowserLauncher2 project. It is derived and somewhat updated from the BrowserLauncher class originally written and released by Eric Albert. I used the original BrowserLauncher class successfully in a multi-platform Java application which ran locally with a web browser interface in the early 2000s.
Note that BrowserLauncher2 is licensed under the GNU Lesser General Public License. If that license is unacceptable, look for a copy of the original BrowserLauncher which has a very liberal license:
This code is Copyright 1999-2001 by Eric Albert (ejalbert#cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that's more out of curiosity than anything and is not required. This software includes no warranty. The author is not repsonsible for any loss of data or functionality or any adverse or unexpected effects of using this software.
Credits:
Steven Spencer, JavaWorld magazine (Java Tip 66)
Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
Projects other than BrowserLauncher2 may have also updated the original BrowserLauncher to account for changes in browser and default system security settings since 2001.
You can also use the Runtime to create a cross platform solution:
import java.awt.Desktop;
import java.net.URI;
public class App {
public static void main(String[] args) throws Exception {
String url = "http://stackoverflow.com";
if (Desktop.isDesktopSupported()) {
// Windows
Desktop.getDesktop().browse(new URI(url));
} else {
// Ubuntu
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);
}
}
}
Hope you don't mind but I cobbled together all the helpful stuff, from above, and came up with a complete class ready for testing...
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MultiBrowPop {
public static void main(String[] args) {
OUT("\nWelcome to Multi Brow Pop.\nThis aims to popup a browsers in multiple operating systems.\nGood luck!\n");
String url = "http://www.birdfolk.co.uk/cricmob";
OUT("We're going to this page: "+ url);
String myOS = System.getProperty("os.name").toLowerCase();
OUT("(Your operating system is: "+ myOS +")\n");
try {
if(Desktop.isDesktopSupported()) { // Probably Windows
OUT(" -- Going with Desktop.browse ...");
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(url));
} else { // Definitely Non-windows
Runtime runtime = Runtime.getRuntime();
if(myOS.contains("mac")) { // Apples
OUT(" -- Going on Apple with 'open'...");
runtime.exec("open " + url);
}
else if(myOS.contains("nix") || myOS.contains("nux")) { // Linux flavours
OUT(" -- Going on Linux with 'xdg-open'...");
runtime.exec("xdg-open " + url);
}
else
OUT("I was unable/unwilling to launch a browser in your OS :( #SadFace");
}
OUT("\nThings have finished.\nI hope you're OK.");
}
catch(IOException | URISyntaxException eek) {
OUT("**Stuff wrongly: "+ eek.getMessage());
}
}
private static void OUT(String str) {
System.out.println(str);
}
}
Its very simple just write below code:
String s = "http://www.google.com";
Desktop desktop = Desktop.getDesktop();
desktop.browse(URI.create(s));
or if you don't want to load URL then just write your browser name into string values like,
String s = "chrome";
Desktop desktop = Desktop.getDesktop();
desktop.browse(URI.create(s));
it will open browser automatically with empty URL after executing a program
I recast Brajesh Kumar's answer above into Clojure as follows:
(defn open-browser
"Open a new browser (window or tab) viewing the document at this `uri`."
[uri]
(if (java.awt.Desktop/isDesktopSupported)
(let [desktop (java.awt.Desktop/getDesktop)]
(.browse desktop (java.net.URI. uri)))
(let [rt (java.lang.Runtime/getRuntime)]
(.exec rt (str "xdg-open " uri)))))
in case it's useful to anyone.
on windows invoke "cmd /k start http://www.example.com"
Infact you can always invoke "default" programs using the start command.
For ex start abc.mp3 will invoke the default mp3 player and load the requested mp3 file.
JavaFX bundles a cross-platform solution inside its StandaloneHostService that is independent of AWT, which is somehow similar to krzysiek.ste's answer.
I rewrote it to include some xdg-open alternatives (which are actually used by xdg-open by the way).
private static final String[][] commands = new String[][]{
{"xdg-open", "$1"},
{"gio", "open", "$1"},
{"gvfs-open", "$1"},
{"gnome-open", "$1"}, // Gnome
{"mate-open", "$1"}, // Mate
{"exo-open", "$1"}, // Xfce
{"enlightenment_open", "$1"}, // Enlightenment
{"gdbus", "call", "--session", "--dest", "org.freedesktop.portal.Desktop",
"--object-path", "/org/freedesktop/portal/desktop",
"--method", "org.freedesktop.portal.OpenURI.OpenURI",
"", "$1", "{}"}, // Flatpak
{"open", "$1"}, // Mac OS fallback
{"rundll32", "url.dll,FileProtocolHandler", "$1"}, // Windows fallback
};
Here is the final Java snippet, avoiding string concatenation and escape character issues.
public static void showDocument(final String uri) {
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Mac OS")) {
Runtime.getRuntime().exec(new String[]{"open", uri});
} else if (osName.startsWith("Windows")) {
Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", uri});
} else { //assume Unix or Linux
new Thread(() -> {
try {
for (String[] browser : commands) {
try {
String[] command = new String[browser.length];
for (int i = 0; i < browser.length; i++)
if (browser[i].equals("$1"))
command[i] = uri;
else
command[i] = browser[i];
if (Runtime.getRuntime().exec(command).waitFor() == 0)
return;
} catch (IOException ignored) {
}
}
String browsers = System.getenv("BROWSER") == null ? "x-www-browser:firefox:iceweasel:seamonkey:mozilla:" +
"epiphany:konqueror:chromium:chromium-browser:google-chrome:" +
"www-browser:links2:elinks:links:lynx:w3m" : System.getenv("BROWSER");
for (String browser : browsers.split(":")) {
try {
Runtime.getRuntime().exec(new String[]{browser, uri});
return;
} catch (IOException ignored) {
}
}
} catch (Exception ignored) {
}
}).start();
}
} catch (Exception e) {
// should not happen
// dump stack for debug purpose
e.printStackTrace();
}
}
There is also
BrowserUtil.browse(uri);
(source code) that seems to be a more compatible with some more exotic setups. I recently had a client where Desktop.getDesktop().browse(uri) was failing in Fedora Linux, but BrowserUtil.browse(uri) worked.
I also believe this is now the preferred way by JetBrains to open a browser (for example, see this thread)
As I had this same problem and found no decent solution for our case so I ported the https://www.npmjs.com/package/open npm package to Java https://github.com/vaadin/open and released it into Maven central. Can be used as
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>open</artifactId>
<version>8.4.0.2</version>
</dependency>
Open.open("https://stackoverflow.com/")
or for a specific browser
Open.open("https://stackoverflow.com/", App.FIREFOX);

Categories

Resources