问题描述
请求:
这是我所在地区的 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!
塞纳里奥:
我正在使用 JavaExe 将应用程序作为 系统服务 在桌面 交互式 功能中运行.确切地说,我有一个捕获桌面屏幕截图的应用程序.我希望它在任何用户登录时运行(以管理员身份),因此没有人可以阻止它.
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 *****) 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.
问题:
根据我的研究,
你必须创建一个类似.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?
我下面的代码 works 作为**的 .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?
推荐答案
你应该有:
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"; } }
对于 UI 部分:
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" }; } }