• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Using Runtime to invoke firefox in new window

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Users,

I am facing an issue invoking the URL in firefox in a new window using Java Runtime


The below code works (This open the URL in the new tab of the existing firefox browser)

Process clientProcess = rt.exec(new String[] {"firefox.exe", "coderanch.com"});

//Stream processing code for Error stream and Input stream

clientProcess.waitFor();

But when I try to open it in a new window like

Process clientProcess = rt.exec(new String[] {"firefox.exe", "-new-window","coderanch.com"});

//Stream processing code for Error stream and Input stream

clientProcess.waitFor();
the url is invoked in a new browser but it fails to block at waitFor() it comes out with an exit code as 1.

If you know about any clues please share with me.

Thanks


 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't reproduce the error. I'm using the following code. I think you must be doing something different. Please post the exact code you are using.

 
samrativ iv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. Actually in my Linux machine when I tried the original scenario I hit the issue that I have mentioned. Now when I check your test program in windows without new window option it opens in the tab of the existing browser. But the waitfor call is not blocking. I am expecting the code to get blocked until the browser is killed. But it exits with 1 option and it means it is a problematic exit.

I have modified your code with what I did. Please let me know if you can think of any issue that is not making this call blocking. ( I am more interested in -new-window option)


import java.io.*;

public class StartFirefox{

public static void main(String args[]){
Runtime rt = Runtime.getRuntime();
try{
Process clientProcess = rt.exec(new String[] {"C:\\Program Files\\Mozilla Firefox\\firefox.exe","coderanch.com"});


StreamProcessor errorSt = new StreamProcessor(clientProcess.getErrorStream(), "ERROR");
StreamProcessor outputSt = new StreamProcessor(clientProcess.getInputStream(), "OUTPUT");

errorSt.start();
outputSt.start();

//Here actually the call should block

int exitcode = clientProcess.waitFor();

System.out.println(" Exit code "+exitcode);

} catch (Exception e){
e.printStackTrace();
}
}


static class StreamProcessor extends Thread
{
InputStream is;
String type;

StreamProcessor(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">>>>" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

}
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can now reproduce the exit code of 1 (edit: did not see your followup post until after):



 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Runtime.exec() won't.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:When Runtime.exec() won't.



If firefox.exe is already running, new calls to firefox.exe will join the existing process. A new tab or window can be created, but proc.waitFor() will not block. The java process does not own it and so waitFor() will not block.

Internet Explorer and Google Chrome will spawn new windows processes for each call to their .exe and so will work as expected (waitFor() will block and return exit code zero when the process is closed)

you can see multiple processes for IE and Chrome in the windows task manager, but only one for firefox.

If I run the code from "when runtime.exec() won't" and point it to a local html file instead of a .exe , it will open it with the default program (firefox), not start a new process, and return an exit code of 0. I do not know why.
 
samrativ iv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, absolutely. This is how Firefox behaves. I have also figured out the same. But if we want to open a new instance of firefox when the other has already opened then we need to create a new profile in Firefox and use that while staring ours. By default Firefox opens with the default profile and if one profile is already in use the new instance can't be created with the same profile as the profile will be locked.

If we create a new profile like "myprofile" then we can start the new instance (-no-remote) of Firefox like

Process clientProcess = rt.exec(new String[] {"C:\\Program Files\\Mozilla Firefox\\firefox.exe","-P","myprofile","-no-remote","coderanch.com"});

I may need to check if in Linux machine also the same behavior is seen
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every body

I have a problem in showing firefox in os linux

I have a page that runs in server-client environemet and I want when I click on this option to show a html in firefox but when i use this code in java there is no any action in client side

I have a class in my code is called browser control

and this class has two method
//check the os
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}



//to show html file in firefox

public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{

if (windows)
{
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
System.err.println("testwindows");
}
else
{
System.err.println(url);
cmd = UNIX_PATH + " " + UNIX_P + url ;

Process p = Runtime.getRuntime().exec(cmd);
System.err.println(cmd);
System.err.println("testunix");
}
}
catch(IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);

}


}

But when i run this jar file of my program in client side I have get the error could not invoke borwser

Anybody knows this problem???

Regards,
sahar

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I understand it correctly that you're running this code on the server and are hoping that it's able to open a browser on the client? If so, that won't work.

But when i run this jar file of my program in client side I have get the error could not invoke borwser


What is "the error"? TellTheDetails.

See https://coderanch.com/how-to/java/Java-FAQ#launch-browser for how open a browser using client-side code.
 
sahar alijannejad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a software that I develop on linux and I want to show and html files and I click on option 987 in my system.In my develop environment ,It shows the html files.But I have a system that has customized linux and it install on branch' bank.When I create Jar file in my program and copy in system is in on branch, bank then this pages is not showed.
If you have any question please ask me?

Regards,
Sahar
 
sahar alijannejad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL,

There is anybody to guide me to solve my problem please?

REgards
 
I child proofed my house but they still get in. Distract them with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic