普通java项目打jar包运行

1.新建一个java项目,必须有main方法,sys的打印目录重定向到到外部文件

public class Main {

	public static void main(String[] args) throws FileNotFoundException {
		File test = new File("log.txt");
		PrintStream out = new PrintStream(new FileOutputStream(test));
		
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				System.setOut(out);
				System.out.println("hello world!"+new Date());
			}
		}, 1000, 1000);

	}

}

2.把项目到出成jar,若是Launch configuration 选不到要执行的main方法类,就先执行一下main方法

3.运行jar文件

方法一:打开cmd界面后输入java -jar ***.jar 就能够运行,而且能够看到jar同级目录生成了log.text日志文档,关掉命令行框,程序终止

方法二:新建bat命令运行工具,双击运行,关闭程序须要在进程里杀掉

@echo off

start javaw -jar tdemo.jar

exit

相关文章
相关标签/搜索