linux下運行java程式 和 最佳化的幾種方式

來源:互聯網
上載者:User
1..直接nohup後台運行jar包建立test.sh 檔案內容如下nohup java -jar ZL_LRC_RES.jar >/dev/null  &注意步驟1:匯出可執行檔jar包,設定好main方法
2:上傳的sh指令碼注意編碼
vi test.sh
set fileformat=unix3:./test.sh執行4:ps -ef|grep java 查看進程5:查看輸出2..通過ant匯出java程式執行ant指令碼匯出jar包後傳到linux伺服器<project name="build" default="build">

<property environment="env" />

<property file="component.properties" />

<property name="debug" value="true" />

<property name="jdk1.6" value="C:/Program Files/Java/jdk1.7.0_01/bin/javac" />
<!-- 設定jdk1.6編譯器的全路徑 -->
<property name="javaCompiler" value="${jdk1.6}" />
<!-- 編譯時間使用的javac -->

<path id="cmp.classpath">
<fileset dir="WebRoot/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
<pathelement path="${java.class.path}" />
</path>

<target name="init">
<echo message="Processing component ${cmp.name}..." />
</target>

<target name="cleanup" depends="init" description="Cleans build directories">
<echo message="Cleaning..." />
<delete includeEmptyDirs="true" failonerror="true">
<fileset dir="build" includes="**/*" />
</delete>
</target>

<target name="compile.check" depends="init">
<mkdir dir="build/depcache" />
<depend srcdir="src/" destdir="build/src" classpath="${cmp.classpath.compile}" cache="build/depcache" closure="yes" />
<depend srcdir="test/" destdir="build/test" classpath="${cmp.classpath.compile}${cmp.classpath.test}" cache="build/depcache" closure="yes" />
<condition property="recompile" value="true">
<not>
<and>
<uptodate>
<srcfiles dir="." includes="src/**/*.java" />
<mapper type="glob" from="*.java" to="build/*.class" />
</uptodate>
<uptodate>
<srcfiles dir="." includes="src/**/*" excludes="**/*.java" />
<mapper type="glob" from="*" to="build/*" />
</uptodate>
</and>
</not>
</condition>
</target>

<target name="compile" depends="compile.check" if="recompile">
<echo message="Compiling sources..." />
<mkdir dir="build/src" />
<!-- 編譯src.home下的所有java檔案。fork被設定為true,這樣可指定編譯使用的JDK,此處使用jdk1.5。必鬚根據java源檔案的編碼來設定encoding。 -->

<javac destdir="build/src" source="1.6" target="1.6" debug="${debug}">
<src path="src/"/>
<!--src path="src/main/resources" /-->
<classpath>
<pathelement path="${cmp.classpath.compile}" />
<path refid="cmp.classpath" />
</classpath>
</javac>
<copy todir="build/src">
<fileset dir="src" excludes="**/*.java" />
<!--fileset dir="src/main/resources" excludes="**/*.java" /-->
</copy>
<echo message="Compiling tests..." />
<mkdir dir="build/test" />
<javac destdir="build/test" source="1.6" target="1.6" debug="${debug}">
<src path="test" />
<!--src path="src/test/resources" /-->
<classpath>
<pathelement location="build/src" />
<pathelement path="${cmp.classpath.test}" />
<!--pathelement path="${jar_path}" />
<pathelement path="${cmp.classpath.compile}" /-->
<path refid="cmp.classpath" />
</classpath>
</javac>
<copy todir="build/test">
<fileset dir="test" excludes="**/*.java" />
<!--fileset dir="src/test/resources" excludes="**/*.java" /-->
</copy>
</target>

<target name="package.check" depends="compile">
<condition property="repackage">
<not>
<uptodate>
<srcfiles dir="build" includes="src/**/*" />
<mapper type="merge" to="${cmp.name}.jar" />
</uptodate>
</not>
</condition>
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm" />
</tstamp>
</target>

<target name="package" depends="package.check" if="repackage">
<echo message="Creating ${cmp.name}.jar..." />
<jar destfile="build/${cmp.name}.jar" index="true">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Class-Path" value="${cmp.classpath.package}" />
<attribute name="Implementation-Title" value="${cmp.name}" />
<attribute name="Implementation-Version" value="${cmp.name} built on ${TODAY}" />
</manifest>
<fileset dir="build/src" />
</jar>
</target>

<target name="build" depends="package" description="Builds component" />

<!-- Packages files tarball for Linux  WebRoot/WEB-INF/lib-->
<target name="cpid.linux.package" depends="build" description="Packages to Linux">

<property name="package.name" value="build/cpid-install.tar.gz" />
<mkdir dir="build/install"/>
<mkdir dir="build/install/lib"/>
<mkdir dir="build/install/config"/>
<echo message="Making a tar file ${package.name}" />
<!-- Copy the needed files into build dir -->
<echo message="Copy files into build directory" />
<copy todir="build/install/lib" overwrite="true">
<fileset dir="WebRoot/WEB-INF/lib/" includes="**/*.jar" />
<fileset file="build/*.jar"></fileset>
</copy>

<copy todir="build/install/config" overwrite="true">
<fileset dir="config/cpid/" includes="**/*" />
</copy>
<copy todir="build/install/" overwrite="true">
<fileset dir="shell/cpid/" includes="**/*" />
</copy>
<!-- Make the package now -->
<tar destfile="${package.name}" longfile="gnu" basedir="build/install/" compression="gzip" />
<!--
<delete includeEmptyDirs="true" failonerror="false" quiet="true">
<fileset dir="${build.dir}" includes="**/*" />
</delete>
-->
</target>

<!-- Packages files tarball for Linux  WebRoot/WEB-INF/lib-->
<target name="top.linux.package" depends="build" description="Packages to Linux">

<property name="package.name" value="build/top-install.tar.gz" />
<delete includeEmptyDirs="true" failonerror="false" quiet="true">
<fileset dir="build/install" includes="**/*" />
</delete>
<mkdir dir="build/install"/>
<mkdir dir="build/install/lib"/>
<mkdir dir="build/install/config"/>
<echo message="Making a tar file ${package.name}" />
<!-- Copy the needed files into build dir -->
<echo message="Copy files into build directory" />
<copy todir="build/install/lib" overwrite="true">
<fileset file="build/*.jar"></fileset>
</copy>

<copy todir="build/install/config" overwrite="true">
<fileset dir="config/top/" includes="**/*" />
</copy>
<copy todir="build/install/" overwrite="true">
<fileset dir="shell/top/" includes="**/*" />
</copy>
<!-- Make the package now -->
<tar destfile="${package.name}" longfile="gnu" basedir="build/install/" compression="gzip" />
<!--
<delete includeEmptyDirs="true" failonerror="false" quiet="true">
<fileset dir="${build.dir}" includes="**/*" />
</delete>
-->
</target>

</project>執行的sh指令碼#! /bin/ksh
. ${HOME}/.profile

# set 設定檔地址
cp="/scmgt/shell/gt/config"

# set ant匯出的jar包
cp="$cp:/scmgt/shell/gt/productServer.jar"

#set 要用到的伺服器上第三方公用jar包 ,如果jar包中有productServer.jar會覆蓋之前的,所以要grep -v 

basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp="$cp:$file"
done
echo $cp
export CLASSPATH=$cp#運行指令碼${1} 是傳入參數 如:./test.sh  3
java -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8 com.sitech.nbc.product.service.impl.ChTFtpMusicFileServiceImpl ${1}3..通過crontab -e 運行java定時任務1.crontab -e 進入定時檔案,編輯

## add by gt 每隔5分鐘執行如下指令碼
*/5 * * * * sh /scmgt/shell/FtpStart.sh >/dev/null 2>&12.FtpStart.sh定時要執行的sh指令碼,(做了防止重複java的機制,如果存在刪除正在執行的,執行新的)同時在執行sh指令碼時候會輸出日誌#! /bin/ksh
. ${HOME}/.profile

count=`ps -ef|grep "FtpColoringMain.sh"|grep -v grep|wc -l`

if [ ${count} -lt 1 ]
then
   nohup sh /scmgt/shell/gt/FtpColoringMain.sh 0 2>&1 >/scmgt/shell/gt/FtpColoringMain.log  &
fi

kill -9 `ps -ef|grep "FtpWalkmanMain.sh"|grep -v grep|awk '{print $2}'`
count=`ps -ef|grep "FtpWalkmanMain.sh"|grep -v grep|wc -l`

if [ ${count} -lt 1 ]
then
   nohup sh /scmgt/shell/gt/FtpWalkmanMain.sh 3 2>&1 >/scmgt/shell/gt/ftpwalkman.log  &
fi3.FtpWalkmanMain.sh#! /bin/ksh
. ${HOME}/.profile

# set classpath
cp="/scmgt/shell/gt/config"

basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
cp="$cp:/scmgt/shell/gt/productServer.jar"

for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp="$cp:$file"
done
echo $cp
export CLASSPATH=$cp
java -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8 com.sitech.nbc.product.service.impl.ChTFtpMusicFileServiceImpl ${1}4.gc最佳化執行java#! /bin/ksh
. ${HOME}/.profile

# set classpath
cp=/scmgt/shell/gt/config

#set main jar
mianjar=/scmgt/shell/gt/test/productServers.jar
cp=$cp:$mianjar

#set other jar
basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp=$cp:$file
done

MAIN_CLASS=com.sitech.nbc.util.est

JAVA_OPTS="-Xms1024m -Xmx1024m  -DSTATUS=$status -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8"

# *********** concurrent mark-sweep GC options  ***********
    JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC"

    #Ratio between eden and survivor spaces
    JAVA_OPTS="$JAVA_OPTS -XX:PermSize=256m"
    JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=10"

    #Sets the threshold percentage of the used heap in the old generation at which the CMS collection takes place.

    JAVA_OPTS="$JAVA_OPTS -XX:CMSInitiatingOccupancyFraction=55"

    #This switch determines how much the objects may age in the young generation before getting promoted to the older generation.
    JAVA_OPTS="$JAVA_OPTS -XX:MaxTenuringThreshold=3"
    #   JAVA_OPTS="$JAVA_OPTS -XX:CMSIncrementalSafetyFactor=20"

    #This is workaround for JVM bugs: 6546278, 2150325, 2150326. Remove after new JVM version is installed
    JAVA_OPTS="$JAVA_OPTS -XX:+UseMembar"

# *********** parallel GC options  ***********
#    JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC"
#    JAVA_OPTS="$JAVA_OPTS -XX:InitialSurvivorRatio=5"
#    JAVA_OPTS="$JAVA_OPTS -XX:-UseAdaptiveSizePolicy"
#    JAVA_OPTS="$JAVA_OPTS -XX:-UsePSAdaptiveSurvivorSizePolicy"
#     JAVA_OPTS="$JAVA_OPTS -XX:TargetSurvivorRatio=90"

# *********** commong GC options ***********

     JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"
     #fix for jre18u crash
     JAVA_OPTS="$JAVA_OPTS -XX:-ReduceInitialCardMarks"

CMD="java -classpath $cp $JAVA_OPTS $MAIN_CLASS $@"

$CMD 2>&1 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.