作为一个Javaweb初学者,Tomcat是一个最长打交道的小型服务器,但是他的启动和关闭很不方便,在一个含有大量文件的bin文件夹中找到startup.bat和shutdown.bat 是一件很令人头疼的事情。
为了解决这个问题,我用Java写了一个小工具。
程序很简单,使用1.6版本的jdk,以及AWT组件布局,代码简单,仅一个类,目前功能还不太完善,仅一个打开,一个关闭,我打算再陆续添加几个功能。
1.Mac支持。因为在Windows环境下tomcat的启动程序是.bat文件,但是在Mac环境下,为.sh文件。实现思路:通过对操作系统的判断,更改相应的后缀名。
2.Webapp添加删除。在tomcat中,添加app需要将相应的文件或war文件拉到webapp文件夹中,这样很麻烦,所以在小工具中添加此功能,将会使操作更加便捷,方便。实现思路:java IO
3.待定。
以下是代码
package org.dgx.tomcat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MainUI {
private static String tomcatUrl=null;
static TextField textField = new TextField(30);
static String path = System.getProperty("user.dir");
static File readFile=new File(path+"\\url.txt");
public void setTomcatUrl(String tomcatUrl) {
this.tomcatUrl = tomcatUrl;
}
public static void main(String[] args) throws IOException {
if (!readFile.exists()){
readFile.createNewFile();
}
// 初始化面板开始----------------------------------------------
Frame f1 = new Frame("Welcome To Tomcat ");
f1.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 50));
f1.setSize(750, 500);
//新建标题1
Label tittle1 = new Label("Welcome To Tomcat");
tittle1.setFont(new Font("宋体", 11, 45));
//新建标题2
Label tittle2 = new Label("Tomcat URL");
tittle2.setFont(new Font("宋体", 11, 30));
// 文本框
//调用本地缓存
Reader reader=new FileReader(readFile);
char[] byteArray = new char[(int) readFile.length()];
int size=reader.read(byteArray);
tomcatUrl=new String(byteArray);
reader.close();
textField.setFont(new Font("宋体", 11, 20));
textField.setText(tomcatUrl);
// 新建按钮
Button bChange = new Buttons("change");
Button bStart = new Buttons("open");
Button bStop = new Buttons("stop");
//新建panel
Panel p1 = new Panel();
p1.add(tittle1);
Panel p2 = new Panel();
p2.add(tittle2);
p2.add(textField);
p2.add(bChange);
Panel p3 = new Panel();
p3.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
p3.add(bStart);
p3.add(bStop);
// 将组件添加到面板上
f1.add(p1);
f1.add(p2);
f1.add(p3);
// 面板可见
f1.setLocationRelativeTo(null);
f1.setVisible(true);
// 初始化面板结束----------------------------------------------
// 事件监听
Monitor m = new Monitor(); // 创建事件监听器m
bChange.addActionListener(m);
bStart.addActionListener(m);
bStop.addActionListener(m); // 将事件源对象b2注册到事件监听器m中,告知b2已被m所监听
textField.addActionListener(m);
f1.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 创建实现了事件监听接口ActionListener的事件监听器类Monitor
static class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = null;
// 事件信息被打包在了ActionEvent
// actionPerformed()方法就是具体的处理事件的方法。
//判断当前操作系统 待完善
if (e.getActionCommand().equals("open")) {
//开启线程
command = tomcatUrl+"\\bin\\startup.bat";//开启tomcat命令
Thread1 t1 = new Thread1();
t1.setCommand(command);
t1.start();
} else if(e.getActionCommand().equals("stop")){
command = tomcatUrl+"\\bin\\shutdown.bat";//关闭tomcat命令
Thread1 t1 = new Thread1();
t1.setCommand(command);
t1.start();
}else {
//创建url缓存
try {
tomcatUrl=textField.getText();
readFile.delete();
readFile.createNewFile();
Writer writer = new FileWriter(readFile);
writer.write(tomcatUrl);
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// ActionCommand中默认信息为该Button的名字
}
}
//重写button,添加样式
static class Buttons extends Button {
private static final long serialVersionUID = 1L;
public Buttons(String label) throws HeadlessException {
super(label);
super.setFont(new Font("宋体", 12, 40));
// TODO Auto-generated constructor stub
}
}
//cmd命令 打开bat文件
static void callCommand(String command) throws IOException {
Runtime runtime = Runtime.getRuntime();//返回与当前的Java应用相关的运行时对象
//指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例
Process process = runtime.exec(command);
runtime.gc();//运行垃圾回收器
String line = null;
String content = "";
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
content += line + "\r\n";
}
System.out.println(content);
}
//新线程
static class Thread1 extends Thread {
private String command;
public void setCommand(String command) {
this.command = command;
}
@Override
public void run() {
try {
callCommand(command);
} catch (IOException e) {
e.printStackTrace();
System.out.println("open failure");
}
}
}
}