2019-01-07 回答
java程序在开发完成后,需要部署到服务器,如果是web项目,需要部署到web服务器,否则部署到应用服务器。
java是跨平台的编程语言,服务器的操作系统可以是windows、linux或者其它,下面将在redhat6操作系统下,详细说明java程序在web服务器和应用服务器上的部署情况。
1、java程序部署在应用服务器
(1) java程序helloworld 在redhat6上部署的目录结构
bin : 存放shell脚本run.sh
conf :存放配置文件log4j.properties
lib :存放jar包helloworld.jar、log4j-1.2.16.jar
logs:存放程序运行日志文件log.log
(2)编写测试类helloworld.java 并打成jar包helloworld.jar
package com.test;
import org.apache.log4j.logger;
import org.apache.log4j.propertyconfigurator;
public class helloworld {
private static logger log = logger.getlogger(helloworld.class);
public static void main(string[] args) {
try{
//log4j.properties变量的值在脚本bin/run.sh 中读取
string config = system.getproperty("log4j.properties");
if (config != null) {
propertyconfigurator.configure(config);
}
log.info("helloworld");
thread thread = new thread(){
public void run(){
while(true){
try {
thread.sleep(5*1000);
log.info("每隔5秒打印一下日志");
} catch (interruptedexception e) {
e.printstacktrace();
log.error(e.getmessage());
}
}
}
};
thread.run();
} catch (exception e) {
log.error("[x]启动失败:"+e.getmessage());
system.exit(1);
}
}
}