我该如何将 JAR 文件复制到启动目录?
我可以使用以下方法:
- 获取正在运行的 JAR 文件路径的方法。 (要复制的文件的路径)。
- 获取当前操作系统的方法。 (Mac、Windows、Linux 或未知)。
我该如何编写一个方法来根据计算机类型将 JAR 文件复制到启动目录?
I have the following methods at my disposal:
- A method to get the running JAR file's path. (The path of the file to copy).
- A method to get the current operating system. (Mac, Windows, Linux, or Unknown).
How would I go about writing a method to copy the JAR file to the startup directory based on the type of computer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将可执行文件放入其中以在启动时运行的“启动目录”概念对于 Linux / UNIX 来说没有意义。
在典型的 Linux 系统中,系统启动时执行的任务由
init
进程基于通常安装在/etc/init.d
中的 shell 脚本进行控制。chkconfig
程序管理“运行级别”目录中的一堆链接,这些链接告诉init
对于每个系统运行级别要运行什么。init.d 脚本不仅仅是任何旧脚本。
我相信现代 UNIX 系统的工作方式大致相同,尽管我知道某些旧版本的工作方式有所不同。
20012 年更新:情况变得更糟。最近的 Linux 发行版正在用
upstart
脚本替换init.d
脚本。叹气。以上涉及系统启动。对于用户来说,也没有“启动目录”的一般概念。
我猜想,窗口管理器可以实现“启动目录”,但典型的 Linux 用户可以自由选择和配置窗口管理器,或者根本不使用窗口管理器。
配置用户登录时发生的事情的正常方法是通过 shell 初始化脚本;例如
~/.bashrc
、~/.bash_login
等等。对于安装程序来说,修改这些文件是一个坏主意,因为在所有情况下都几乎不可能做到正确......考虑到在 shell 初始化脚本中可以做的奇怪而奇妙的事情。但底线是,如果您正在编写安装程序来为多个平台安装软件,则必须了解这些平台。您需要使用它们和(在某种程度上)管理它们的实践经验,以及进行软件安装的“正确方法”的具体知识。显然您还不具备 Linux/UNIX 方面的知识。
在没有足够知识和充分测试的情况下编写安装程序非常非常危险。安装程序必须在特权级别运行,这意味着如果出现问题,它可能会对目标系统造成严重损坏。
The concept of a "startup directory" that you can drop executables into to run at startup time does not make sense for Linux / UNIX.
In a typical Linux system, tasks performed on system startup are controlled by the
init
process based on shell scripts that are typically installed in/etc/init.d
. Thechkconfig
program manages a bunch of links in "run level" directories that tellinit
what to run for each system run level.The init.d scripts are not just any old script.
chkconfig
program when the script should be run; i.e. at what run levels, and in what order.I believe that modern UNIX systems work roughly the same way, though I known that some older versions did this differently.
Update in 20012: It gets worse. Recent Linux distros are replacing
init.d
scripts withupstart
scripts. Sigh.The above deals with system startup. There is no general concept of a "startup directory" for users either.
I guess, a window manager could implement a "startup directory", but a typical Linux user is free to choose and configure the window manager, or use no window manager at all.
The normal way to configure things to happen when the user logs in is via shell initialization scripts; e.g.
~/.bashrc
,~/.bash_login
and so on. It is a BAD IDEA for an installer to modify those files because it would be next to impossible to get it right in all circumstances ... given the weird and wonderful things it is possible to do in a shell initialization script.The bottom line though is that if you are writing installers to install your software for a number of platforms, you have to understand those platforms. You need hands on experience using them and (to some level) administering them, and specific knowledge of the "right way" to do software installation. Clearly you don't have that knowledge yet for Linux / UNIX.
Writing installers without adequate knowledge and adequate testing is really, really dangerous. An installer has to be run at a privilege level that means that it can do serious damage to the target system if something goes wrong.
使用 Apache Commons IO 的 FileUtils 将文件(如果您有路径和名称)复制到任何地方,只要您有权在目的地写入即可。
Use Apache Commons IO's FileUtils to copy the file (if you have the path and the name) to anywhere as long as you have permissions to write in destination.
不知怎的,这个问题听起来像是在问一个错误的方法。为什么不从它所在的位置启动 jar - java -jar /path/to/the/jar.jar 或 java -cp /path/the/jar.jar mainpackage。 MainClass
在 Linux 上,启动目录 - 如果您打开 shell,则通常启动的目录 - 是您的
$HOME
,别名/home/$USER
,或只是当前目录.
或$(pwd)
或$PWD
从那里。因此,如果没有人更改起始目录,则会将 jar 复制到当前目录。
但是,如果您将启动器放在桌面上,则可能会选择 $HOME/Desktop 作为启动目录。
在 Linux 上,没有官方的启动目录;没有人使用这个表达方式。
我们正在谈论自动启动?
如果您想在服务器上运行程序,
/etc/init.d
可能是放置程序的正确位置,或者更好的是程序的启动脚本,该脚本将转到/usr/local/...
或/opt/
或/var
,具体取决于 Linux 风格和用户偏好。如果您的程序需要
X
(使用awt/swing/swt
),则需要等待X
出现,然后/ etc/X11/Xsession.d
将是放置启动脚本的地方。根据运行所需的资源,还会有进一步的考虑。
如果它使用网络资源,并且应根据网络启动或关闭或电源管理采取操作,则再次需要查找不同的目录。
根据 Stephen C. 的说法,如果您没有任何 Linux 经验,您应该向我们提供更多信息。你的程序是做什么的?我们能看到吗?它是开源的吗?它使用哪些计算机资源?客户端还是服务器? GUI 还是控制台?由谁来运行?
Somehow this question sounds like asking for a wrong way to do it. Why don't you start the jar from where it is -
java -jar /path/to/the/jar.jar
orjava -cp /path/the/jar.jar mainpackage.MainClass
On Linux, the startup-dir - the dir, where you normally start , if you open a shell - is your
$HOME
, alias/home/$USER
, or just the current directory.
or$(pwd)
or$PWD
from there.Would therefore copy the jar to the current dir, if nobody changed the starting dir.
However, if you place a starter on the desktop, that might choose
$HOME/Desktop
as the startup-dir.On Linux, there is no official startup-dir; nobody uses this expression.
We're talking about autostart?
If you want to run a program on the server,
/etc/init.d
might be the right place to put your program, or better a starting script for your program, which would go to/usr/local/...
or/opt/
or/var
, depending on Linux flavour and user preferences.If your program needs
X
(usesawt/swing/swt
) it will need to wait forX
to come up, then/etc/X11/Xsession.d
would be the place to put the startscript.Depending on resources it needs to run, there would be further considerations.
If it uses networking resources, and actions should be taken, depending on network start or shutdown, or on powermanagement, there are again different directories, to look for.
According with Stephen C., if you don't have any experience with Linux, you should give us much more information. What does your program do? Can we see it? Is it OpenSource? Which computer resources does it use? Client or server? GUI or console? Run by whom?