0

I have a simple java class as shown below.

public class Launcher {

    public static void main(String[] args) {
        launchURLInBrowser("http://google.com");
    }

    private static void launchURLInBrowser(String url) {
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"C:\\Users\\Administrator\\Downloads\\chrome-win\\chrome.exe", "--kiosk", url});
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

The only thing I am doing in the class is to launch chromium web browser in a kiosk mode and it works fine.

Now, I need to bundle this class in jar file and distribute it. I am going to distribute jar alongside the web browser zip. So, I need to put relative path instead of the hard code path I have given now.

My jar distribution will have following folder structure. Chrome browser will not be put inside the jar. It will be external but inside the same folder as shown below.

launcher.jar
chrome-win
    -other files and folders
    -chrome.exe

How can I do this?

yetanothercoder
  • 385
  • 2
  • 6
  • 15
  • Either put the relative path as you are doing with the absolute one (if you are sure it stays the same all the time), or pass the path as an argument (and then use it like `private static void launchURLInBrowser(String chromeExePath, String url)`). – deHaar Mar 23 '20 at 08:26
  • Since inside a ZIP/JAR file the `chrome.exe` won't be a "real" file from the point of view of the OS, you'll have to unpack it to some temporary location and launch it from there (or just use the OS browser using [`Desktop.browse`](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Desktop.html#browse(java.net.URI))). – Joachim Sauer Mar 23 '20 at 08:34
  • @JoachimSauer No. I don't have zip inside the jar. See I made an edit to the question. – yetanothercoder Mar 23 '20 at 09:06

2 Answers2

0

I suppose you are using Windows OS.
I would prefer the folder like that

launcher.bat
jre
lib
   -launcher.jar
chrome-win
   -other files and folders
   -chrome.exe

In launcher.bat

  1. get your current path using bat , and using it as the RootPath for your Java
  2. set up Java home(RootPath/jre), classpath(RootPath/lib), etc
  3. execute Launcher.java, using the path chrome.exe and url as parameter of the main method.

Hope that can help.

Xiao Yu
  • 244
  • 1
  • 8
-1

I think you can try using

getClass().getClassLoader().getResource("Downloads\\chrome-win\\chrome.exe")

instead of absolute path... Or trying this kind of logic

cybronele
  • 31
  • 5