linux 通过脚本执行java程序

[size=large]最近在linux上定时执行脚本程序的要求越来越多了,今天的任务是:在linux服务器上编一个脚本,并定时执行该脚本(脚本的目的执行一个java程序,要求java程序要打成jar包)。[/size]

1.编写java程序(公司要求我要读取一个txt文件,并将txt文件的不同的ip做一个统计在存放到一个新的txt文件中)
package com.easymorse;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

public class Test1 {

/**
* author jordy
*/
public static void main(String[] args) {


HashSet set = new HashSet();
HashSet set1 = new HashSet();
String temp = "";
boolean tf;
try {
//要读取的txt文件
FileReader fr = new FileReader("WebRoot\\resource\\welcomelog.txt");
BufferedReader in = new BufferedReader(fr);

String line;
while ((line = in.readLine()) != null){
temp = line.substring(0, 14);
tf = set.add(temp);

if(tf){
int firstleft = line.indexOf('(');
int firstright = line.indexOf(')')+1;
String othercontent = line.substring(firstleft, firstright);
String allcontent = temp + " -*-*-*-*-*-*-*-*- " + othercontent + "\n";
set1.add(allcontent);
}

}

set = null;
Iterator it = set1.iterator();
while(it.hasNext()){
String content = (String)it.next();
//新文件保存的路径
write("D:\\123.txt",content);
}




} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}




}


public static void write(String path,String content){
String s = new String();
String s1 = new String();

try{
File f = new File(path);
if(f.exists()){
System.out.println("文件存在");
}else{
System.out.println("文件不存在,正在创建...");
if(f.createNewFile()){
System.out.println("文件创建创建成功");
}else{
System.out.println("文件创建创建失败");
}
}

BufferedReader input = new BufferedReader(new FileReader(f));
while((s = input.readLine()) != null){
s1 += s + "\n";
}

System.out.println("文件以前的内容:" + s1);
input.close();

s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();


}catch(Exception e){
e.printStackTrace();

}


}


}


由于要统计不同的IP,代码中应用了HashSet来存放IP地址。

上述java程序是在windows下编写的,如果在linux服务器上运行,只需要把上面文件的路径和文件更换了就可以了。

2.编写好java程序后,将java程序打成jar文件(环境linux)

我将上述测试好的java类上传到了linux服务器上,考虑到服务器的路径与windows下的路径不同所以我在服务器上将上述代码更改为服务器上的代码了。

(1) 修改文件路径
sudo vim Test1.java


(2) 生成.class文件

javac Test1.java


(3) 创建一个.mf文件,并编辑该文件
sudo touch Test1.mf


sudo vim Test1.mf


Test1.mf文件内容如图:

[img]http://dl.iteye.com/upload/attachment/258616/e312fdf7-0f5a-3c47-a750-2db52b3ba68c.jpg[/img]

(4) 打.jar包

jar cfm test1.jar Test1.mf *.class


执行完上述命令,文件夹中会多一个test1.jar文件

java -jar test1.jar


测试你的jar是否成功,如果成功会有一个123.txt文档。

(5) 编写一个linux脚本执行第四步命令(写此脚本的目的是方便以后linux可以定时执行脚本)

[color=violet]新建一个.sh文件[/color]
sudo touch test.sh


[color=violet]编辑该test.sh文件[/color] 文件内容如图:

[img]http://dl.iteye.com/upload/attachment/258629/3489add4-52d4-3c5c-a257-8b1076048b1d.jpg[/img]

sudo chmod 777 test.sh

./test.sh


上面就把linux执行java程序介绍完了,关于如何执行脚本可以参考我的另一篇博客[url]http://jordy.easymorse.com/?p=59[/url] 本文用的的文件可以在附件中下载其中welcomelog.txt为原始文件,123.txt为程序生成文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值