0

With Java I am able to open Firefox just fine with a URL I set using this code:

Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe" + " " + "http://www.website.com");

But if possible how can I then return the following data?

  1. If Firefox loaded successfully? (As a bool)
  2. If the URL(s) loaded successfully? (As a bool, or if using more than 1 URL as a bool array)
  3. The time taken to load (As a value/string, or if using more than 1 URL as a value/string array.
  4. The contents of the Firefox Web Console (As a string, or if using more than 1 URL as a string array)

Is there a way to do this and get back this type of data from Firefox or other programs that .exec is launching?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zeddex
  • 1,260
  • 5
  • 21
  • 38
  • 1
    Don't do that. You cannot assume that your users have Firefox, or that they even have a `C:` drive. – SLaks Aug 12 '12 at 17:23
  • I want this for my own use, so Firefox will be found. – zeddex Aug 12 '12 at 17:55
  • @zeddex - Guideline: don't hardcode stuff if you don't have to ;) – paulsm4 Aug 12 '12 at 18:12
  • The `Desktop` class will launch the default browser and provide an answer to point 1. But still not .1% of what is required for an automation tool that can interact with/observe *'other programs'*. – Andrew Thompson Aug 13 '12 at 11:24

2 Answers2

3

You're simply not going to get very much mileage from Java Runtime.exec(), IMHO...

Here's an alternative that should let you do some/most of what you're trying to do ... from Perl (not Java):

That's just an example. There are many, many other alternatives. If you want to go the Java route, search for "screen scrapers" or "automation". For example:

'Hope that helps

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Perl would be good if i was using that but this needs to be Java because that is the programming language i was using. I am making a automation tool but want to learn to do this myself rather than use a library which is why i asked. Thanks for the reply though. – zeddex Aug 12 '12 at 18:03
  • @zeddex - To paraphase Yoda: "Do or do not... there is no Runtime.exec()" ;) – paulsm4 Aug 12 '12 at 18:10
  • @zeddex - Guideline: don't reinvent the wheel if you don't have to. Not only do you "get the job done", but you get a chance to learn from a successful design - and maybe invent something better! – paulsm4 Aug 12 '12 at 18:15
  • Getting back what seems to be fairly standard response information from exec is not exactly reinventing the wheel though. There is probably ways to use a while loop and program check to test if the program is running but i was thinking there would be a solution built in to java for this since it can start the programs. – zeddex Aug 12 '12 at 18:28
  • If i were to use selenium or another library though, how can it get all of the data i requested in my original question. Do you know or have links to the java code which can do that if possible? thanks – zeddex Aug 12 '12 at 18:30
  • *"making a automation tool"* Java is the ***wrong*** language. – Andrew Thompson Aug 13 '12 at 11:21
1

I would recommend to take a look at Selenium... Basically, you need (of course) a monitor for the firefox process, and since Firefox does not offer COM you need sth "inside" firefox, i.e. a plugin. This is what selenium does. Such a plugin then could send you any internals for instance through a simple tcp connection.

monnoo
  • 301
  • 3
  • 3