How to invoke the browser and then output result of a java program into a html page displayed in the browser

Posted on 2005-04-11
Last Modified: 2008-03-03
Dear all:

I have a java program running on a PC that need to invoke the default browser (ideally prompt the user to download a browser: e.g. IE or Mozilla if user does not have a compatible browser installed) in the PC and output results (e.g. string text) from the java program into a html page (like tables or hyperlinks etc. in the html page) that will be displayed in the browser.

Anyone please provide example codes or suggestion how to do that would be greatly appreciated! Thanks.

best regards,

Question by:yi02
    LVL 19

    Accepted Solution


    I think this might be what you are looking for:

    You'd probably generate the HTML into a local file and then supply a file URL when starting the browser so it will display what has been generated.

    Jim Cakalic
    LVL 1

    Expert Comment

    If you're sure you're running on a Windows PC (though this would be very OS dependent, obviously) you can do:
        Runtime.getRuntime ().exec (new String[] {"explorer.exe", fileName});
    Where fileName is the name of the file you want to display.
    LVL 9

    Expert Comment

    First, a question, what's wrong with using Swings HTML browsing classes?

    Secondly, you can locally write your output to an HTML file and then open it using Runtime.exec, however, instead of trying to execute a Browser's executable (i.e. explorer.exe), I'd try to invoke the HTML file itself and usually, that should get the OS to launch the currently configured Browser.

    Author Comment

    Dear doronb:

    Your comments sound very much what I want. Could you please help me with a piece of sample codes. Appreciate it very much!


    LVL 9

    Assisted Solution

    A sample code of using Swings HTML browsing classes can be found here:


    You can refer to this article about running external processes with Java:
    LVL 19

    Expert Comment

    by:Jim Cakalic
    So is it display HTML in a Swing component that you want or starting an external browser to display an HTML file? If using Swing, great. If you want to start an external browser then I'd suggest the material at the link I first posted is more directly related to that problem.

    Author Comment

    Dear jim_cakalic:

    Very Sorry, I was away for a little away and just got a chance back to this question.

    Regarding your question: So is it display HTML in a Swing component that you want or starting an external browser to display an HTML file?
    Either ways are fine as long as the displayed content is in html format (e.g. if there is a hyperlink in the displayed html, clicking on it should jump to the intended url pointed by the hyperlink etc).

    For the material at the link you first posted is Window-oriented, Is there more generic way (i.e. platform-independent)? Sorry I may mislead you by saying I run my program in PC, in fact, my program can run on other platform too, such as macintoch etc.

    Thanks a lot.



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    This video teaches viewers about errors in exception handling.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now