通过Windows快捷方式传递参数运行.jar [英] Run .jar through Windows shortcut passing arguments

查看:1039
本文介绍了通过Windows快捷方式传递参数运行.jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Windows快捷方式(.lnk)传递参数来运行.jar?

Is it possible run .jar through Windows shortcut (.lnk) passing arguments?

推荐答案

在Java 7中,是的。您可以将快捷方式指向 c:\windows \ system32 \ javaava.exe javaw.exe as适当的并包含您在命令行中使用的相同参数。

In Java 7, yes. You can point the shortcut to c:\windows\system32\java.exe or javaw.exe as appropriate and include the same arguments you would use on the command line.

在干净的Java 8安装中,不容易。不幸的是,Java 8不再将副本 java.exe javaw.exe 放入系统文件夹,而是放置符号链接在ProgramData文件夹中。 Windows不喜欢符号链接的快捷方式;有时他们工作,有时他们没有。 (即使相同的快捷方式可能适用于某些用户帐户,但不适用于其他用户帐户。)

In a clean Java 8 installation, not easily. Unfortunately Java 8 no longer puts copies java.exe and javaw.exe into the system folder, but instead puts symbolic links in a ProgramData folder. Windows doesn't like shortcuts to symbolic links; sometimes they work, sometimes they don't. (Even the same shortcut might work for some user accounts but not for others.)

(看起来如果您将Java 8安装在顶部Java 7它保留了旧的行为,但我还没有彻底调查过。)

(It seems that if you install Java 8 over top of Java 7 it retains the old behaviour, but I haven't investigated this thoroughly yet.)

这个简单的启动器可能很有用;您可以使用在javaw.exe快捷方式中使用的相同命令行参数创建一个或多个快捷方式。

This simple launcher may be useful; you can create one or more shortcuts to it with the same command line parameters you would have used in the shortcut to javaw.exe.

#include <Windows.h>

void NoCRTMain(void)
{
    wchar_t * cmdline = GetCommandLineW();
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    GetStartupInfo(&si);

    if (!CreateProcess(L"C:\\ProgramData\\Oracle\\Java\\javapath\\javaw.exe", cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        MessageBox(NULL, L"Unable to launch Java.", L"runjava.exe", MB_OK);
    }

    ExitProcess(0);
}

要在Visual Studio中编译,您需要更改一些项目设置:

To compile in Visual Studio, you will need to change some project settings:


  • C / C ++代码生成中的缓冲区安全检查结果

  • 忽略所有默认库为是链接器输入

  • 链接器高级中NoCRTMain的入口点

  • 链接器高级中的无随机基址到否(见评论

  • Buffer Security Check to No in C/C++ Code Generation
  • Ignore All Default Libraries to Yes in Linker Input
  • Entry Point to NoCRTMain in Linker Advanced
  • Randomized Base Address to No in Linker Advanced (see commentary here)

(或者您可以将主函数从NoCRTMain更改为WinMain,但是您需要安装C运行时或静态链接它。)

(Or you can change the main function from NoCRTMain to WinMain, but then you need to install the C runtime or link it statically.)

Windows 10中的附加:,如果您有两个指向同一可执行文件的开始菜单快捷方式,则在开始菜单中只能看到其中一个。因此,在这种情况下,您需要有多个启动器副本,每个快捷方式一个。

Additional: in Windows 10, if you have two Start Menu shortcuts pointing to the same executable, only one of them will be visible in the Start Menu. So in this case you need to have multiple copies of the launcher, one for each shortcut.

这篇关于通过Windows快捷方式传递参数运行.jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆