Java app behind proxy to use http_proxy variable in linux
Don't forget the shell variable _JAVA_OPTIONS
export _JAVA_OPTIONS='-Dhttp.proxyHost=cache.com -Dhttp.proxyPort=3128'
For more properties look here:http://mindprod.com/jgloss/properties.html
You can use this script to automatic enviroment passing to java application
This is an intelligent script, if you enable nmap section, it is detecting proxy up or down status , if it is up status it is using proxy if it is down status it is using direct connection..
With this script you can connect your app with enviroment settings or overwrite enviroment or with proxy service up detection method , the application selects direct or proxy mode
This is an intelligent connection bash shell script
Ofcouse if you don't enable nmap service up/down section, this is an simple proxy enviroment or your overwrite value for your application
It is producing automaticly proxy connection command line then running your java application
This is script's code:
#!/bin/bash# Author : Kerim BASOL# Twitter : http://twitter.com/kerimbasol# URL : http://kerimbasol.com# Version : 0.1# Java Proxy support script# You can use with GNU License# Which is your runtime jar file# Please change this as your application's needs JARFILE="myapp.jar"#Automaticly import system proxy settingsif [ -n "$http_proxy" ] ; then echo $http_proxy | grep "@" if [ $? -eq 0 ]; then # If variable has username and password, its parse method different PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/.*@\(.*\):.*/\1/') PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*@.*:\(.*\)/\1/' | tr -d "/") USERNAME=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $1}') PASSWORD=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $2}') else # If it doesn't have username and password, its parse method this PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/\(.*\):.*/\1/') PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*:\(.*\)/\1/' | tr -d "/") fifi# If you want to overwrite system proxy settings# uncomment these lines as your wish#PROXY_HOST="127.0.0.1"#PROXY_PORT="3128"#USERNAME="kerimbasol"#PASSWORD="deneme"# Display usageif [ $# -gt 0 ] ; then if [ $1 = "--help" ] ; then echo "$0 [<proxy-server> <proxy-port> [<username> <password> ] ] " exit 0 fifi# Command line proxy passif [ $# -gt 1 ] ; then PROXY_HOST=$1 PROXY_PORT=$2 if [ $# -gt 3 ] ; then USERNAME=$3 PASSWORD=$4 fifi# If you want to use this feature , enables and disables proxy support for proxy service up or down status# uncomment these line, if you installed nmap# at ubuntu system you can type this command for this future# sudo apt-get install nmap#STATUS=$(nmap -sT $PROXY_HOST -p $PROXY_PORT 2>/dev/null| grep open |awk '{print $2}')#if [ "$STATUS" != "open" ]; then # If service isn't running, disable proxy support# PROXY_HOST=""# PROXY_PORT=""#fiCMD="java -cp."if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ] ; then CMD="java -cp . -Dhttp.proxyHost=$PROXY_HOST -Dhttp.proxyPort=$PROXY_PORT" if [ -n "$USERNAME" -a -n "$PASSWORD" ]; then CMD="$CMD -Dhttp.proxyUser=$USERNAME -Dhttp.proxyPassword=$PASSWORD" fifi# If you want , change this line as your application wish ;)CMD="$CMD -jar $JARFILE"eval $CMD
With a current JVM you can pass the proxy host and port using Java properties
java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080 -Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL
See http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html