Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.
I am behind a proxy server. How do I set my JVM to use the proxy ?
Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.
I am behind a proxy server. How do I set my JVM to use the proxy ?
Also, if you are always looking to download the same schema, then you can add the schema to your classpath (filesystem or JAR), and then use a custom EntityResolver
See here for a more complete discussion of this approach.
Edit: See @me.yahoo.com/a/0QMxE's discussion of CatalogResolver, which uses the EntityResolver approach:
If you are counting on retrieving schemas or DTDs over the internet, you're building a slow, chatty, fragile application. What happens when that remote server hosting the file takes planned or unplanned downtime? Your app breaks. Is that OK?
See http://xml.apache.org/commons/components/resolver/resolver-article.html#s.catalog.files
URL's for schemas and the like are best thought of as unique identifiers. Not as requests to actually access that file remotely. Do some google searching on "XML catalog". An XML catalog allows you to host such resources locally, resolving the slowness, chattiness and fragility.
It's basically a permanently cached copy of the remote content. And that's OK, since the remote content will never change. If there's ever an update, it'd be at a different URL. Making the actual retrieval of the resource over the internet especially silly.
The following shows how to set in Java a proxy with proxy user and proxy password from the command line, which is a very common case. You should not save passwords and hosts in the code, as a rule in the first place.
Passing the system properties in command line with -D and setting them in the code with System.setProperty("name", "value") is equivalent.
But note this
Example that works:
But the following does not work
The only difference is the position of the system properties ! (before and after the class)
If you have special characters in password, you are allowed to put it in quotes "@MyPass123%", like in the above example.
If you access an HTTPS service, you have to use https.proxyHost, https.proxyPort etc.
If you access an HTTP service, you have to use http.proxyHost, http.proxyPort etc.
From the Java documentation (not the javadoc API):
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
Set the JVM flags
http.proxyHost
andhttp.proxyPort
when starting your JVM on the command line. This is usually done in a shell script (in Unix) or bat file (in Windows). Here's the example with the Unix shell script:When using containers such as JBoss or WebLogic, my solution is to edit the start-up scripts supplied by the vendor.
Many developers are familiar with the Java API (javadocs), but many times the rest of the documentation is overlooked. It contains a lot of interesting information: http://download.oracle.com/javase/6/docs/technotes/guides/
Update : If you do not want to use proxy to resolve some local/intranet hosts, check out the comment from @Tomalak:
JVM uses the proxy to make HTTP calls
This may use user setting proxy
That works for me:
P/S: I base on GHad's answer.