如何用java开发linux下的服务程序

lyb_73 2003-08-17 12:39:04
欲开发一个linux值守程序,类似于windows下的服务程序,怎么能做到隐藏终端窗口
,在linux下java demo.main &启动后关闭这个终端窗口,程序也关掉了, 有什么办法让他一直在后运行?以及开机时自动启动?(setdameon不太好使)
...全文
219 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lEFTmOON 2003-08-19
  • 打赏
  • 举报
回复
up
收藏
lyb_73 2003-08-19
  • 打赏
  • 举报
回复
好象在linux下只要写个shell script就行了,也可用nohup ...来做
conning333 2003-08-17
  • 打赏
  • 举报
回复
给你up吧!!!
littlethree 2003-08-17
  • 打赏
  • 举报
回复
/*
监视log文件变化线程
*/
class SpyThread extends java.lang.Thread{

public JTextArea jTextArea1=null;


public SpyThread(JTextArea jt){
jTextArea1=jt;
}
//是否监视文件
public static boolean spy_Loop = true;

//监视频率(毫秒)
public final static int SPY_MILLIS =1000 ;

public void run(){
while (spy_Loop){
//System.out.println(java.lang.Thread.currentThread()+Long.toString(System.currentTimeMillis()));
try{
java.lang.Thread.currentThread().sleep(SPY_MILLIS);
spyLog();
}catch(InterruptedException e){}

}
}


public void spyLog(){
String s=jTextArea1.getText();
int chars_read =0;
int size2 = 0;
try{
//根据文件名建立一个文件对象
File file = new File(SpyLogFrame.currFileName);
int size = (int)file.length();
//System.out.println(">>"+size);

//文件长度有变化
if(s.length() !=size){
//如果是文件长度增加,只动态改变增加部分
if(s.length()<size){
//原始长度
chars_read =s.length();
//增加长度
size2=size-s.length() ;
}
//否则如果是尺寸减少了,则重新刷新
else if(s.length()>=size){
chars_read =0;
size2=size;
s="";
}
}
if(size2>0||size==0){
//根据file对象建立一个input reader来读取数据
FileReader in = new FileReader(file);
//创建一个字符数组存放文件内容
char[] data = new char[size];
//从缓存中读取数据
in.read(data, 0, size);

in.close();

//数据内容给编辑器
s=s+(new String(data, chars_read, size2));

jTextArea1.setText(s);
JComponent j=jTextArea1;
j.setAutoscrolls(true);
}
}
catch (IOException e) { }
}

/**
* 停止监视
*/
public void stopSpy(){
spy_Loop=false;
}

/**
* 启动监视
*/
public void StartSpy(){
spy_Loop=true;
run();
}

}
littlethree 2003-08-17
  • 打赏
  • 举报
回复
看个例子吧:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class SpyLogFrame extends JFrame {
//这个面板将包括编辑器的主面板
JPanel contentPane;
//菜单+工具条+编辑区+状态条的布局对象
BorderLayout borderLayout1 = new BorderLayout();

//菜单条=====================================
JMenuBar menuBar1 = new JMenuBar();
//文件菜单
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
//帮助菜单
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
//动态定义的菜单对象
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();


//工具条======================================
JToolBar toolBar = new JToolBar();
JToolBar toolBarSlider = new JToolBar();

//按纽
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
//按纽图标
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
ImageIcon image4;
ImageIcon image5;
ImageIcon image6;

//文本编辑区============================================
//带滚动条的面板
JScrollPane jScrollPane1 = new JScrollPane();
//文本框
JTextArea jTextArea1 = new JTextArea();

//状态条==========================================
JLabel statusBar = new JLabel();

//文件打开对话框对象
JFileChooser jFileChooser1 = new JFileChooser();


public static String currFileName = null; // Full path with filename. null means new/untitled.

//是否脏读
public static boolean dirty = true;


//监视频率(毫秒)
public static int SPY_MILLIS =100 ;
//监视线程
java.lang.Thread spy_thread =new java.lang.Thread(new SpyThread( jTextArea1),"Spy_log0");

Document document1;
TitledBorder titledBorder1;
JSlider jSlider1 = new JSlider();
JLabel jtitle=new JLabel("监视频率:");

//构造函数
public SpyLogFrame(String currFileName) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//System.out.println(AWTEvent.WINDOW_EVENT_MASK);
this.currFileName=currFileName;
try {
//初始化窗体布局
jbInit();
//更新窗口标题
updateCaption();
}
catch(Exception e) {
e.printStackTrace();
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧