java接受参数快捷键,通过Windows快捷方式传递参数运行.jar

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

解决方案

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.

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.)

(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.)

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

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);

}

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

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)

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值