简体   繁体   English

在后台运行的Java程序

[英]Java program running in background

I have a simple java program which is just a single piece of code that reads from a database and modifies the contents of the database based on certain conditions. 我有一个简单的java程序,它只是从数据库中读取的一段代码,并根据某些条件修改数据库的内容。 Now, what I want is that this program should start automatically at the startup and silently run in the background unless someone kills it from the task manager. 现在,我想要的是这个程序应该在启动时自动启动并在后台静默运行,除非有人从任务管理器中杀死它。 I have never done something like this before and don't know exactly how to go about it. 我之前从未做过这样的事情,也不知道该如何去做。 Can someone help me out as to how this can be done? 有人可以帮我解决这个问题吗? Thank you.. 谢谢..

Follow the these steps to do the job :(Assuming you are using windows and jre is installed ) 按照以下步骤完成工作:(假设您正在使用Windows和jre安装)

  • First compile your java program and place the class file at one location. 首先编译java程序并将类文件放在一个位置。
  • Now create a bat file and place java LOCATION TO THAT CLASS FILE/MyProgram in that. 现在创建一个bat文件并将java LOCATION TO THAT CLASS FILE/MyProgram
  • Put your bat file in start up programs 将您的bat文件放入启动程序中
  • Restart the system, you will get your program running in back ground..! 重新启动系统,您的程序将在后台运行..! Hope this will help you. 希望这会帮助你。

There are two problems here 这里有两个问题

  1. How to add this program to the startup 如何将此程序添加到启动

  2. Run the program as a daemon (background process) 将程序作为守护程序运行(后台进程)

    • Simplest way to do is using a while loop and sleep for required time interval in the while loop. 最简单的方法是使用while循环并在while循环中休眠所需的时间间隔。 Then perform the database operation. 然后执行数据库操作。

Also for windows, you can check this JSL http://www.roeschter.com/ 也适用于Windows,您可以查看此JSL http://www.roeschter.com/

Thanks. 谢谢。

首先创建jar bash,然后将其添加到crontab任务列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM