Bonjour à tous,
j'aimerais savoir si il y a un moyen de faire ouvrir un navigateur WEB par programmation java.
Merci d'avance :D:D:D
Version imprimable
Bonjour à tous,
j'aimerais savoir si il y a un moyen de faire ouvrir un navigateur WEB par programmation java.
Merci d'avance :D:D:D
Code:BrowserControl.displayURL("http://www.javaworld.com")
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public class BrowserControl { /** * Display a file in the system browser. If you want to display a * file, you must include the absolute path name. * * @param url the file's url (the url must start with either "http://" or * "file://"). */ public static void displayURL(String url) { boolean windows = isWindowsPlatform(); String cmd = null; try { if (windows) { // cmd = 'rundll32 url.dll,FileProtocolHandler http://...' cmd = WIN_PATH + " " + WIN_FLAG + " " + url; Process p = Runtime.getRuntime().exec(cmd); } else { // Under Unix, Netscape has to be running for the "-remote" // command to work. So, we try sending the command and // check for an exit value. If the exit command is 0, // it worked, otherwise we need to start the browser. // cmd = 'netscape -remote openURL(http://www.javaworld.com)' cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")"; Process p = Runtime.getRuntime().exec(cmd); try { // wait for exit code -- if it's 0, command worked, // otherwise we need to start the browser up. int exitCode = p.waitFor(); if (exitCode != 0) { // Command failed, start up the browser // cmd = 'netscape http://www.javaworld.com' cmd = UNIX_PATH + " " + url; p = Runtime.getRuntime().exec(cmd); } } catch(InterruptedException x) { System.err.println("Error bringing up browser, cmd='" + cmd + "'"); System.err.println("Caught: " + x); } } } catch(IOException x) { // couldn't exec browser System.err.println("Could not invoke browser, command=" + cmd); System.err.println("Caught: " + x); } } /** * Try to determine whether this application is running under Windows * or some other platform by examing the "os.name" property. * * @return true if this application is running under a Windows OS */ public static boolean isWindowsPlatform() { String os = System.getProperty("os.name"); if ( os != null && os.startsWith(WIN_ID)) return true; else return false; } /** * Simple example. */ public static void main(String[] args) { displayURL("http://www.javaworld.com"); } // Used to identify the windows platform. private static final String WIN_ID = "Windows"; // The default system browser under windows. private static final String WIN_PATH = "rundll32"; // The flag to display a url. private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; // The default browser under unix. private static final String UNIX_PATH = "netscape"; // The flag to display a url. private static final String UNIX_FLAG = "-remote openURL"; }
Salut,
Il y a également la solution JDIC : Desktop.browse(URL);
a++
merci beaucoup pour ces réponses rapides!! :D:D:D:D
Je me lance dans les tests!!! :wink:
Bonjour,
J'ai effectivement reussi à ouvrir une fenetre à partir de Java et aller sur url désirée.
Mais Je voudrais lancer une url sans ouvrir de navigateur. Est-ce possible ?
Merci de vos réponses
Que veux tu dire par là exactement ?
Sinon pour compléter les réponses ci-dessus qui sont un peu ancienne, il est désormais possible de faire cela en standard avec Java 6 via la méthode Desktop.browse()...
a++
je suis dans un programme java, et je voudrais passer des paramatres via une url sans ouvrir une nouvelle fenêtre.
Bonjour à tous,
Je me permet de répondre, même si la question ne m'est pas destinée, car je fais face au même problème :
Je souhaiterai appeler des url depuis une application java. Ces url vont servir à créer des éléments sur une application web jee donc je n'ai pas la nécessité d'ouvrir un browser, je souhaite juste déclencher des actions struts via une url, est-ce possible ?
Salut,
Si je comprends bien, tu veux juste envoyer une requête http à une adresse, le résultat n'étant pas une page html à afficher ?
Si c'est cela et bien passe par HttpURLConnection, ou utilise la bibliothèque HttpClient de Apache, ca te simplifiera la vie.
try {
Desktop d = Desktop.getDesktop();
d.browse(new URI("http://www.developpez.net/"));
} catch (URISyntaxException e) {
} catch (IOException e) {
}
Bonjour,
Je reviens sur ce sujet car, contrairement à l'auteur, je souhaite bien lancer le navigateur par défaut sur une URL précise. J'ai trouvé deux solutions sur le net :Or, ces deux méthodes ont un comportement étrange quand il s'agit d'accéder à une URL avec le protocole file et qui contient un signet.Code:
1
2 Desktop.getDesktop().browse(url);//cas vu plus haut Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+url);//exécution d'une commande dos
En http pas de problème quand vous lancer ça dans une console :
Par contre, si vous copiez cette page sur votre bureau et que vous exécutez cette commande :Code:rundll32 url.dll,FileProtocolHandler http://fr.wikipedia.org/wiki/Uniform_Resource_Locator#URL_absolue
Le navigateur ne recevra pas le signet...Code:rundll32 url.dll,FileProtocolHandler file://C:/Users/sasétoua/Desktop/Uniform_Resource_Locator#URL_absolue
En passant le signet dans la barre d'adresse du navigateur, ça fonctionne évidemment.
Quelqu'un a une idée sur ce comportement ?
Pour info, mon but est d'ouvrir une aide web depuis mon appli java sur le bon sujet en fonction d'où je suis dans mon appli.
J'ai l'impression qu'il y a un bug dans les navigateurs, ou alors dans FileProtocolHandler, ou alors c'est une question de sécurité dans le cas des URLs en file://, que je ne comprends pas.
En tout cas, si tu entoures l'URL de " et ", ça marche mieux. À noter que ce problème et sa solution s'observent directement dans la console DOS, donc ça n'a rien à voir avec Java. (Bon, sauf Desktop.browse(), solution officielle Java, qui ne fonctionne pas, puisque n'utilisant apparemment pas cette astuce.)
Code:rundll32 url.dll,FileProtocolHandler "file:///C:/fichier.html#truc"
Code:Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler \""+url+"\"");
Merci pour cette réponse.
Finalement, de mon côté, j'ai pu mettre ma doc sur mon serveur donc j'accède maintenant en http.