JavaExe和Java应用程序作为Windows系统服务与桌面交互 [英] JavaExe and Java application as windows system service interactive to desktop

查看:91
本文介绍了JavaExe和Java应用程序作为Windows系统服务与桌面交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请求:

这是Java开发人员在我的语言环境中面临的一个非常普遍的问题.我真的为此困扰了很多天.搜索并尝试了很多,阅读文档.阅读与JavaExe相关的所有stackoverflow问题.请仅在您之前做过类似的事情并且有一个完整的答案时回复.我将非常感谢社区!

This is a very common problem faced by Java devs in my locale. I am really stuck for many days on this. Searched and tried a lot, read the docs. read ALL the stackoverflow questions related to JavaExe. Please only reply if you have done similar thing before and have a comprehensive answer. I would be really grateful to the community!

Senario:

我正在使用 JavaExe 以桌面 interactive 功能作为系统服务运行应用程序. 确切地说,我有一个捕获台式机屏幕截图的应用程序.我希望它以任何用户登录名(以管理员身份身份运行),因此没有人可以阻止它.

I am using JavaExe to run an application as system service in desktop interactive capability. To be exact I have an application that captures screenshots of desktops. I want it to run (as admin) on any user login so no one can stop it.

我有一个myapp.jar,settings.txt和一个lib目录.

I have a myapp.jar, settings.txt and a lib dir.

我已经搜索了很多并发现JavaExe的作品(通过观看其示例)

I have searched alot and found JavaExe works (by watching its examples)

如果有人有更好的方法.请说明.

If anyone has a better way. Please state so.

问题:

根据我的研究

  1. 您必须创建一个名为.exe的.properties文件,并在该文件中写入"RunType = 1".

您在主类中定义了一个静态方法:serviceInit()

you define a static method in your main class : serviceInit()

我需要放置任何类或参考/导入吗?怎么样?

Do I need to place any class or reference/import? How?

下面的我的代码可以独立运行 .jar,也可以在javaExe.exe 中运行.

My code below works as stand alone .jar and in javaExe.exe too.

它现在提供系统服务,并以 SYSTEM 用户身份运行.但它与桌面不互动.即它不显示任何GUI.

It now does makes a system service and runs by as SYSTEM user. but It is NOT interactive to desktop. i.e its not showing any GUI.

package temp;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class Temp {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


          serviceInit();

    }

    public static boolean serviceInit(){

        new Thread(){
            public void run(){
                Integer i = 0;
                while(i < 999999999){
                    JOptionPane.showMessageDialog(null,i);
                    i++;

                }
            }
        }.start();

        return true;
   }




}

我不认为可以将.jar,lib目录和settings.txt捆绑到一个.exe中吗?

And I dont think that bundling the .jar, lib directory and settings.txt into one .exe is possible?

推荐答案

在您的情况下,您应该拥有:

you should have in your case :

public class MyApp_ServiceManagement
{
    static boolean isMsgToDisplay = false;

    /////////////////////////////
    public static boolean serviceInit()
    {
        (new Thread()
        {
            public void run()
            {
                for(int i=0;i < 6;i++)
                {
                     try { sleep(5*1000); }
                     catch(Exception ex) {}

                     isMsgToDisplay = true;
                }
            }
        }).start();

        return true;
    }

    /// is Data ready to be send to the UI ?
    public static boolean serviceIsDataForUI()
    {
        return isMsgToDisplay;
    }

    /// Data to be send to the UI
    public static Serializable serviceDataForUI()
    {
        isMsgToDisplay = false;
        return "hello, I am an interactive Service";
    }
}

,对于用户界面部分:

public class MyApp_TaskbarManagement
{
    /// To show (or not) the icon in tray
    public static boolean taskIsShow()
    {
        return false;
    }

    /// Receive the message from Service
    public static void taskDataFromService(Serializable data)
    {
        JOptionPane.showMessageDialog(null, data);
    }

    /// descr of UI
    public static String[] taskGetInfo()
    {
        return new String[]
            {
                "UI part of Service"
            };
    }
}

这篇关于JavaExe和Java应用程序作为Windows系统服务与桌面交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆