如何在服务器上运行main方法
程序员文章站
2022-04-28 10:58:38
...
1. 新建maven项目,编写测试类,和main方法,在pom.xml文件中添加如下内容:
<packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.8</jdk.version> </properties> <!-- 添加相应的依赖包 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <encoding>${project.build.sourceEncoding}</encoding> <classesDirectory>target/classes</classesDirectory> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>./</classpathPrefix> <mainClass>com.crt.member.sersordata.MemberService</mainClass> </manifest> </archive> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
2. 通过mvn clean package 对该项目进行打包,依赖的包会打在target/lib目录下
3. 编写start.sh执行脚本
#!/bin/bash source /etc/profile if [ "`ps -ef |grep crt-apigateway.jar|grep -v grep |awk '{print $2}'`" ];then for pid in `ps -ef |grep crt-apigateway.jar|grep -v grep |awk '{print $2}'`;do kill -9 $pid done fi #export JAVA_HOME=/app/jdk export PATH=$JAVA_HOME/bin:$PATH APP_HOME=/tmp/sersordata/target/lib for i in "$APP_HOME"/*.jar do CLASSPATH="$CLASSPATH":"$i" done export CLASSPATH=.:$CLASSPATH echo ${CLASSPATH} echo $JAVA_HOME java -Xms2048M -Xmx2048M -Xmn512M -Xss256K -XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:SoftRefLRUPolicyMSPerMB=0 -Dfile.encoding=UTF-8 -Xms32m -Xmx200m com.crt.member.sersordata.MemberService -classpath ${CLASSPATH}
4. 把项目包和start.sh放到服务器目录下,执行start.sh文件,main方法将会得到执行。
上一篇: js中阻止事件冒泡的方法
下一篇: Linux查看日志命令(转)