1

I hava a java program with two buttons, one for chrome and one for firefox. I press one of them, and the browser starts at some particualar location on the screen and with smalles size.

i have try running terminal commands, something like this

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  --profile-directory="Default" --app="data:text/html,<html><body><script>window.moveTo(198,60);window.resizeTo(1167,708);window.location='https://stackoverflow.com';</script></body></html>"

And it works, but only for chrome. I want at least chrome and firefox on both windows and linux.

Searching a little I have come across other solution. Running javascript on java, somethig like:

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");             
    engine.eval("window.open('https://stackoverflow.com')");
    engine.eval("window.resizeTo(800,600)");

But Im getting compilation errors:

ReferenceError: "window" is not defined in <eval> at line number 1

And I don´t know whats going. Ideas?

zqxwce dsa
  • 11
  • 1
  • 4
  • 2
    Did you try wrapping the url string in some quotes? – Capricorn Aug 02 '18 at 19:28
  • okay, thanks! Now Im getting ReferenceError: "window" is not defined in at line number 1 – zqxwce dsa Aug 02 '18 at 19:33
  • 1
    Possible duplicate of [How to open the default webbrowser using java](https://stackoverflow.com/questions/5226212/how-to-open-the-default-webbrowser-using-java) – Reinstate Monica Cellio Aug 02 '18 at 19:35
  • So why are you not using command line switches to set the position and size? – epascarello Aug 02 '18 at 19:36
  • 2
    A JavaScript engine running inside the JVM will most likely have no global objects, so no DOM like you'd find in the browser. That's why it doesn't know what you mean by `window`. That's a browser-specific concept. – Daniel Earwicker Aug 02 '18 at 19:44
  • @zqxwcedsa check the answer and tell me if it worked for you. – Oghli Aug 04 '18 at 12:12
  • @DanielEarwicker you are right actually `ScriptEngineManager` runs the script on server side. window is a client-side object so he can't access it. – Oghli Aug 04 '18 at 12:14

2 Answers2

0

ScriptEngineManager runs the script on server side. window is a client-side object, you can't access it from server.

in another word since you are not executing your script in a browser, the window object is not defined.

You can try this way to open a website on your default browser of the operating system:

     Desktop desktop=Desktop.getDesktop();
     URI url = new URI("http://somewhere");
     desktop.browse(url);

to open a non-default browser in Java you should use Runtime.exec()

for Windows OS try this it worked for me:

     String browserPath = "C:/Program Files/Mozilla Firefox/firefox.exe";
     String url = "http://somewhere";
     try {
             String[] b = {browserPath, url};
             Runtime.getRuntime().exec(b);
          }
      catch (Exception exc) {
              exc.printStackTrace();
      }

for further information on how to use Runtime.exec() on others OS read here

Oghli
  • 2,200
  • 1
  • 15
  • 37
0

For Windows, you can do something like this using Runtime:

Runtime rt = Runtime.getRuntime();
rt.exec("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe stackoverflow.com");

I believe that you can do something similar for Google Chrome. I took a look to a code I implemented in the past for Chrome and it was a little different, but previous approach should work as well:

Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{"cmd", "/c","start chrome http://www.stackoverflow.com"});

If you would like to do it for a Linux based OS, then you can use Runtime as well:

Runtime rt = Runtime.getRuntime();
rt("/usr/bin/firefox -new-window http://www.stackoverflow.com");

I remember I got some references from this page:

https://www.mkyong.com/java/open-browser-in-java-windows-or-linux/

Hope it can help you.

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51