这是我想出的。它有效,但我想知道是否有更优雅的东西。谢谢!
米莎
/* Copyright (c) 2010 Misha Koshelev. All Rights Reserved.
*
* TODO:
* - Add Linux support
*/
package com.mksoft.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Run specified class within this JAR file on system startup.
*
* @author Misha Koshelev
*/
public class RunOnSystemStartup {
/*
* Constants
*/
protected final static String osName=System.getProperty("os.name");
protected final static String fileSeparator=System.getProperty("file.separator");
protected final static String javaHome=System.getProperty("java.home");
protected final static String userHome=System.getProperty("user.home");
/*
* Debugging
*/
protected static boolean debugOutput=false;
protected static void debug(String message) {
if (debugOutput) {
System.err.println(message);
System.err.flush();
}
}
/*
* Helpers
*/
protected static File getJarFile() throws URISyntaxException {
return new File(RunOnSystemStartup.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
protected static File getStartupFile() throws Exception {
debug("RunOnSystemStartup.getStartupFile: osName=\""+osName+"\"");
if (osName.startsWith("Windows")) {
Process process=Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v Startup");
BufferedReader in=new BufferedReader(new InputStreamReader(process.getInputStream()));
String result="",line;
while ((line=in.readLine())!=null) {
result+=line;
}
in.close();
result=result.replaceAll(".*REG_SZ[ ]*","");
debug("RunOnSystemStartup.getStartupFile: Startup Directory=\""+result+"\"");
return new File(result+fileSeparator+getJarFile().getName().replaceFirst(".jar",".bat"));
} else if (osName.startsWith("Mac OS")) {
return new File(userHome+"/Library/LaunchAgents/com.mksoft."+getJarFile().getName().replaceFirst(".jar",".plist"));
} else {
throw new Exception("Unknown Operating System Name \""+osName+"\"");
}
}
/*
* Methods
*/
/**
* Returns whether this JAR file is installed to run on system startup.
*/
public static boolean isInstalled() throws Exception {
return getStartupFile().exists();
}
/**
* Install the specified class from the current JAR file to run on system startup.
*
* @param className Name of class within the current JAR file to run on system startup.
* @param windowTitle Title to display in window title bar, if applicable.
*/
public static void install(String className,String windowTitle) throws Exception {
File startupFile=getStartupFile();
PrintWriter out=new PrintWriter(new FileWriter(startupFile));
if (osName.startsWith("Windows")) {
out.println("@echo off");
out.println("start \""+windowTitle+"\" \""+javaHome+fileSeparator+"bin"+fileSeparator+"java.exe\" -cp "+getJarFile()+" "+className);
} else if (osName.startsWith("Mac OS")) {
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
out.println("<plist version=\"1.0\">");
out.println("<dict>");
out.println(" <key>Label</key>");
out.println(" <string>com.mksoft."+getJarFile().getName().replaceFirst(".jar","")+"</string>");
out.println(" <key>ProgramArguments</key>");
out.println(" <array>");
out.println(" <string>"+javaHome+fileSeparator+"bin"+fileSeparator+"java</string>");
out.println(" <string>-cp</string>");
out.println(" <string>"+getJarFile()+"</string>");
out.println(" <string>"+className+"</string>");
out.println(" </array>");
out.println(" <key>RunAtLoad</key>");
out.println(" <true/>");
out.println("</dict>");
out.println("</plist>");
} else {
throw new Exception("Unknown Operating System Name \""+osName+"\"");
}
out.close();
}
/**
* Uninstall this JAR file from the system startup process.
*/
public static void uninstall() throws Exception {
File startupFile=getStartupFile();
if (startupFile.exists()) {
startupFile.delete();
}
}
}
最佳答案
它是服务还是用户应用程序?如果服务看一下 Java Service Wrapper .
关于java - 我想在 Mac OS/Windows 的系统启动时运行我的 Java 程序。我怎样才能做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2949649/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
一、引擎主循环UE版本:4.27一、引擎主循环的位置:Launch.cpp:GuardedMain函数二、、GuardedMain函数执行逻辑:1、EnginePreInit:加载大多数模块int32ErrorLevel=EnginePreInit(CmdLine);PreInit模块加载顺序:模块加载过程:(1)注册模块中定义的UObject,同时为每个类构造一个类默认对象(CDO,记录类的默认状态,作为模板用于子类实例创建)(2)调用模块的StartUpModule方法2、FEngineLoop::Init()1、检查Engine的配置文件找出使用了哪一个GameEngine类(UGame
之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/