Webページを開くためのjava GUIを取得しようとしています。だからguiは何かをしてhtmlファイルを生成するいくつかのコードを実行します。 このファイルを作成するとすぐに、このファイルをWebブラウザ(Firefox)に開くようにします。 どうすればいいですか?WebブラウザでWebページを開くためのjava GUIの取得
34
A
答えて
39
Java 6以上を使用している場合は、Desktop API、特にbrowseを参照してください。このように使用してください(テストされていません):
// using this in real life, you'd probably want to check that the desktop
// methods are supported using isDesktopSupported()...
String htmlFilePath = "path/to/html/file.html"; // path to your new file
File htmlFile = new File(htmlFilePath);
// open the default web browser for the HTML page
Desktop.getDesktop().browse(htmlFile.toURI());
// if a web browser is the default HTML handler, this might work too
Desktop.getDesktop().open(htmlFile);
0
私はBrowserLauncher2を成功させました。テストされたすべてのプラットフォームでデフォルトのブラウザが呼び出されます。私はJNLP経由でソフトウェアをデモするためにこれを使用します。ソフトウェアは、ユーザのブラウザを情報ページ/フィードバックなどにダウンロードして実行し、駆動する。
JDK 1.4以上。
+0
ユーザーアクティビティのためにページのURLが変更されると、BrowserLauncher2を使用してその新しいURLを取得できますか? –
26
あなたがデフォルトのウェブブラウザでウェブページをJavaプログラムで開く場合は、このコードを試してみてください。
/// file OpenPageInDefaultBrowser.java
public class OpenPageInDefaultBrowser {
public static void main(String[] args) {
try {
//Set your page url in this string. For eg, I m using URL for Google Search engine
String url = "http://www.google.com";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
}
/// End of file
2
私は、これらの回答のすべてが基本的に質問に答えていることを知っていますが、ここではうまくいかない方法のコードがあります。文字列は、htmlファイル
/**
* If possible this method opens the default browser to the specified web page.
* If not it notifies the user of webpage's url so that they may access it
* manually.
*
* @param url
* - this can be in the form of a web address (http://www.mywebsite.com)
* or a path to an html file or SVG image file e.t.c
*/
public static void openInBrowser(String url)
{
try
{
URI uri = new URL(url).toURI();
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(uri);
} else {
throw new Exception("Desktop not supported, cannout open browser automatically");
}
}
catch (Exception e)
{
/*
* I know this is bad practice
* but we don't want to do anything clever for a specific error
*/
e.printStackTrace();
// Copy URL to the clipboard so the user can paste it into their browser
StringSelection stringSelection = new StringSelection(url);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
// Notify the user of the failure
WindowTools.informationWindow("This program just tried to open a webpage." + "\n"
+ "The URL has been copied to your clipboard, simply paste into your browser to access.",
"Webpage: " + url);
}
}
関連する問題
- 1. デフォルトのブラウザでWebページを開く
- 2. java多くのWebページのWebコンテンツを取得
- 3. WebサイトデバッグのためのブラウザでPHPファイルのHTMLコンテンツを開く
- 4. JavaクラスでWebページを取得
- 5. フルスクリーンのPythonでWebブラウザを開く
- 6. AndroidのWebブラウザでユーザーから取得したURLを開く方法
- 7. EclipseのWebブラウザでHTMLファイルを開く
- 8. フレックスアプリケーションでWebページを開く
- 9. jEditorPaneでローカルWebページを開く
- 10. WebKitブラウザでWebページの高さを取得する方法
- 11. ブラウザで外部Webページを開く方法は?
- 12. QtモバイルアプリケーションでWebブラウザを開く
- 13. Java Web開発のための習得コース?
- 14. ユーザーの設定に関係なく、Lotus NotesクライアントのWebブラウザでWebページを開くようにします。
- 15. VB.NETで特定のサイズの既定のブラウザでWebページを開く
- 16. Webブラウザを開き
- 17. C++アプリケーションと通信するためのWebページの取得
- 18. Webブラウザ用の推奨Web開発プラグイン
- 19. Webブラウザでhttpリクエストを取得する
- 20. JavaでWebページのDOMビューを取得するだけでなく、ソース
- 21. を開き、デフォルトのWebブラウザ
- 22. WebブラウザからTIFFファイルを開く
- 23. winformからWebブラウザを開く
- 24. Java Web StartでFirefoxを強制的にページを開くには?
- 25. file_get_htmlでWebページのソースを取得
- 26. IPアドレスでローカルホストのWebページを取得
- 27. HaskellでWebページのステータスコードを取得
- 28. Python - Flask - デフォルトのブラウザで2つのWebページを開きます。
- 29. クロム用javaを使用したWebブラウザ用プラグインの開発
- 30. iOSアプリケーション内のWebページを開く
ねえの場所することができ
注意。まだそれを働かせることはできません。次のエラーが表示されます。「エラーメッセージ:指定されたパスが見つかりません」これは私が正確な経路を与えているにもかかわらずです。 –
@ TheSpecialOne - ファイルに_local_パスを指定していますか?例:Windowsの場合、htmlFilePathは "c:\ path \ to \ file.html"のようになります。 URL構文を使用しないでください... –