How to run and optimize Java programs in Linux

Source: Internet
Author: User
1 .. run the jar package in the nohup background to create test. the sh file content is as follows: nohup Java-jar zl_lrc_res.jar>/dev/null & note Step 1: export the executable jar package and set the main method.
2: Pay attention to encoding for uploaded sh scripts
VI test. Sh
Set fileformat = unix3 :. /test. sh execution 4: PS-Ef | grep Java view process 5: View output 2 .. run the ant script to export the jar package from the ant Java program and upload it to the Linux server <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"/>
<! -- Set the full path of the jdk1.6 compiler -->
<Property name = "javacompiler" value = "$ {jdk1.6}"/>
<! -- Javac used for compilation -->

<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 multiple deemptydirs = "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"/>
<! -- Compile all java files under SRC. Home. Fork is set to true, so that JDK for compilation can be specified. JDK is used here. Encoding must be set based on the Java source file 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" nodes des = "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 multiple deemptydirs = "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 multiple deemptydirs = "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 multiple deemptydirs = "true" failonerror = "false" quiet = "true">
<Fileset dir = "$ {build. dir}" includes = "**/*"/>
</Delete>
-->
</Target>

</Project> Sh script executed #! /Bin/KSh
. $ {Home}/. Profile

# Set Configuration File address
CP = "/scmgt/Shell/GT/config"

# Set ant jar package exported
CP = "$ CP:/scmgt/Shell/GT/productserver. Jar"

# Set the third-party public jar package on the server to be used. If the jar package contains productserver. jar, it will overwrite the previous one, so grep-V is required.

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 # Run the script $ {1} to input parameters such as./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 .. use crontab-e to run Java scheduled tasks 1. crontab-E: Enter the Scheduled File and edit it.

# Add by GT execute the following script every five minutes
*/5 * sh/scmgt/Shell/ftpstart. sh>/dev/null 2> & 12. ftpstart. SH: Specifies the sh script to be executed at a scheduled time. (A mechanism is implemented to prevent repeated Java operations. If a new one is deleted, logs are output when the sh script is executed #! /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> & 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 optimization execution 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 elastic vor Spaces
Java_opts = "$ java_opts-XX: permsize = 256 m"
Java_opts = "$ java_opts-XX: Export vorratio = 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,215 0325, 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: initial?vorratio = 5"
# Java_opts = "$ java_opts-XX:-useadaptivesizepolicy"
# Java_opts = "$ java_opts-XX:-usepsadaptivesurvivorsizepolicy"
# Java_opts = "$ java_opts-XX: target1_vorratio = 90"

# *********** Commong GC options ***********

Java_opts = "$ java_opts-XX: + disableexplicitgc"
# Fix for jre18u crash
Java_opts = "$ java_opts-XX:-performanceinitialcardmarks"

Cmd = "Java-classpath $ CP $ java_opts $ main_class $ @"

$ Cmd 2> & 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.