linux 编译并运行Java文件
因工作需要,调接口时本地访问不了目标服务器,所以只能在服务器上运行调试。
写了个脚本来编译运行。参数输入Java文件名无后缀
#!/bin/bash if [ ! -n "$1" ]; then echo "Please enter a file name!" exit 0 fi JAVA_FILE=$1.java LIB_DIR=`pwd`/lib LIB_JARS=`ls ${LIB_DIR}|grep .jar|awk '{print "'${LIB_DIR}'/"$0}'|tr "\n" ":"` echo -e "starting compile ${JAVA_FILE}...\c" #UNIX shell使文件描述符0与进程的标准输入相结合,文件描述符1与标准输出相结合,文件描述符2与标准出错输出相结合。 #该处的 2>&1 代表着错误输出 输出到 标准输出 COMPILE_RESULT=`javac -classpath ${LIB_JARS} ${JAVA_FILE} 2>&1` if [ -n "$COMPILE_RESULT" ]; then echo "" echo "$COMPILE_RESULT" exit 0 fi echo "Complie OK!" JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true " JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " STDOUT_FILE=`pwd`/log/$1.log CLASS_FILE=$1 echo -e "Starting the ${CLASS_FILE} ...\c" java ${JAVA_OPTS} ${JAVA_MEM_OPTS} -classpath ${LIB_JARS} ${CLASS_FILE} > ${STDOUT_FILE} 2>&1 echo "" echo "Run OK!"