Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

java - Run .jar through Windows shortcut passing arguments

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Java 7, yes. You can point the shortcut to c:windowssystem32java.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 <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);
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...