Search code examples
windowsgradlejava-8gradle-pluginlaunch4j

Gradle Launch4J EXE not trusted by Windows 10


Please note: I have created this GitHub project right here that can be used to perfectly reproduce the problem I'm seeing.


Java 8 here attempting to use Launch4J via the gradle-launch4j Gradle plugin to build a Windows native EXE application. I am doing the development of a Java Swing app on my Mac but the app must run as a Windows EXE on Windows 10. I am also using ShadowJar to build my self-contained "fat jar".

I can build my (Swing) app's fat jar and then run it on my Mac via java -jar build/lib/myapp.jar. It starts and runs no problem.

Here is my Gradle config for Launch4J:

launch4j {
    mainClassName = 'com.example.windows.hello.HelloWindowsApp'
    icon = "${projectDir}/icon.ico"
    jdkPreference = 'jdkOnly'
    initialHeapSize = 128
    jreMinVersion = '1.8.0'
    jreMaxVersion = '1.8.9'
    maxHeapSize = 512
    stayAlive = false
    bundledJre64Bit = true
    bundledJrePath = '../hello-windows/jre8'
}

When I run ./gradle clean build shadowJar createExe createDistro it produces:

hello-windows.zip/
    hello-windows.exe       -->     The Windows EXE built by the 'createExe' task
    lib/*                   -->     The lib/ dir for the EXE that is also built by the `createExe` task 
    jre8/                   -->     OpenJDK JRE8 (copied from the libs/jre8 dir)

So I copy that ZIP file and port it over to a Windows 10 (64-bit) machine. I extract the ZIP and run the EXE by double clicking it inside Windows Explorer (which I can confirm does see the EXE as an Application type). First I see this:

enter image description here

Why is this happening? Are there any Launch4J configurations/settings I can change so that this doesn't happen?

Thanks in advance!


Solution

  • Your first question is more like a Windows question. When you unzip an application from a zip file, Windows will naturally mark it as unsafe, in fact if you check the application properties tab, you will see a checkbox where you can remove that unsafe attribute. It's same as running chmod+x for an executable script in Linux.

    For the second part, I assume you are using the gradle plugin for Launch4j, there are two main ways to configure Launch4j assuming your project folder is structured commonly with the jre library in the same folder containing your executable folder.

    1. By specifying the path only like

      ../jre
      
    2. By specifying the full relative path

      ../jre/bin/javaw.exe
      

    Your generated xml at the end should look like this in the first case.

    <jre>
        <path>../jre</path>
    </jre>
    

    The main point is that the path to JRE is relative to the position of the executable not the current directory. In this case, we step back one directory from the executable folder to the folder containing jre.