Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BrowserLauncher.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.launcher.util;
  5. import java.io.BufferedInputStream;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. /**
  11. * This class opens default browser for DemoLauncher class. Default browser is
  12. * detected by the operating system.
  13. *
  14. */
  15. public class BrowserLauncher {
  16. /**
  17. * Open browser on specified URL.
  18. *
  19. * @param url
  20. */
  21. public static void openBrowser(String url) {
  22. final Runtime runtime = Runtime.getRuntime();
  23. boolean started = false;
  24. final String os = System.getProperty("os.name", "windows")
  25. .toLowerCase();
  26. // Linux
  27. if (os.indexOf("linux") >= 0) {
  28. // See if the default browser is Konqueror by resolving the symlink.
  29. boolean isDefaultKonqueror = false;
  30. try {
  31. // Find out the location of the x-www-browser link from path.
  32. Process process = runtime.exec("which x-www-browser");
  33. BufferedInputStream ins = new BufferedInputStream(
  34. process.getInputStream());
  35. BufferedReader bufreader = new BufferedReader(
  36. new InputStreamReader(ins));
  37. String defaultLinkPath = bufreader.readLine();
  38. ins.close();
  39. // The path is null if the link did not exist.
  40. if (defaultLinkPath != null) {
  41. // See if the default browser is Konqueror.
  42. File file = new File(defaultLinkPath);
  43. String canonical = file.getCanonicalPath();
  44. if (canonical.indexOf("konqueror") != -1)
  45. isDefaultKonqueror = true;
  46. }
  47. } catch (IOException e1) {
  48. // The symlink was probably not found, so this is ok.
  49. }
  50. // Try x-www-browser, which is symlink to the default browser,
  51. // except if we found that it is Konqueror.
  52. if (!started && !isDefaultKonqueror) {
  53. try {
  54. runtime.exec("x-www-browser " + url);
  55. started = true;
  56. } catch (final IOException e) {
  57. }
  58. }
  59. // Try firefox
  60. if (!started) {
  61. try {
  62. runtime.exec("firefox " + url);
  63. started = true;
  64. } catch (final IOException e) {
  65. }
  66. }
  67. // Try mozilla
  68. if (!started) {
  69. try {
  70. runtime.exec("mozilla " + url);
  71. started = true;
  72. } catch (final IOException e) {
  73. }
  74. }
  75. // Try konqueror
  76. if (!started) {
  77. try {
  78. runtime.exec("konqueror " + url);
  79. started = true;
  80. } catch (final IOException e) {
  81. }
  82. }
  83. }
  84. // OS X
  85. if (os.indexOf("mac os x") >= 0) {
  86. // Try open
  87. if (!started) {
  88. try {
  89. runtime.exec("open " + url);
  90. started = true;
  91. } catch (final IOException e) {
  92. }
  93. }
  94. }
  95. // Try cmd /start command on windows
  96. if (os.indexOf("win") >= 0) {
  97. if (!started) {
  98. try {
  99. runtime.exec("cmd /c start " + url);
  100. started = true;
  101. } catch (final IOException e) {
  102. }
  103. }
  104. }
  105. if (!started) {
  106. System.out.println("Failed to open browser. Please go to " + url);
  107. }
  108. }
  109. }