我该如何将 JAR 文件复制到启动目录?

发布于 2024-11-30 12:11:36 字数 162 浏览 0 评论 0原文

我可以使用以下方法:

  • 获取正在运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-12-07 12:11:37

您可以将可执行文件放入其中以在启动时运行的“启动目录”概念对于 Linux / UNIX 来说没有意义。

在典型的 Linux 系统中,系统启动时执行的任务由 init 进程基于通常安装在 /etc/init.d 中的 shell 脚本进行控制。 chkconfig 程序管理“运行级别”目录中的一堆链接,这些链接告诉 init 对于每个系统运行级别要运行什么。

init.d 脚本不仅仅是任何旧脚本。

  • 他们必须遵守特定的“协议”;即支持某些常用选项。
  • 它们通常有特殊的注释,告诉 chkconfig 程序何时应该运行脚本;即在什么运行级别以及以什么顺序运行。

我相信现代 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. The chkconfig program manages a bunch of links in "run level" directories that tell init what to run for each system run level.

The init.d scripts are not just any old script.

  • They have to obey a specific "protocol"; i.e. support certain comman options.
  • They typically have special comments that tell the 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 with upstart 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.

甚是思念 2024-12-07 12:11:37

使用 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.

可可 2024-12-07 12:11:37

不知怎的,这个问题听起来像是在问一个错误的方法。为什么不从它所在的位置启动 jar - java -jar /path/to/the/jar.jar 或 java -cp /path/the/jar.jar mainpackage。 MainClass

Linux 上,启动目录 - 如果您打开 shell,则通常启动的目录 - 是您的 $HOME,别名 /home/$USER,或只是当前目录.$(pwd)$PWD 从那里。

cp JARFILE $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 or java -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.

cp JARFILE $PWD 

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 (uses awt/swing/swt) it will need to wait for X 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文