Record it (encrypt, package, install, run for Windows)

Few people use JavaFX to write desktop applications, but for some reason they have to and need to distribute the installation package. In order to prevent the code from being seen (since jar packages are like zip packages, unzip them to see everything), xJAR is used for encryption.

xjar_github

Start with the github readme and add the xjar-plugin.

// Package command
mvn clean package- dxjar. password= User-defined passwordCopy the code

After packaging, an xjar.go file will appear, which is the execution file. This file is used to execute java-jar ***. Xjar to run the JAR package, thus running the encrypted JavafX program

After Maven is packaged, there are several key files in the target directory

  • ***.jar — unencrypted JAR package
  • ***.xjar — encrypted JAR package
  • Xjar. go — Actually executes the file

Go Java -jar ***.xjar

At this point, you also need to package these files into an installation package, and you need an executable file to run after installation. Because xjar.go is a real run file, it is necessary to compile xjar.go into.exe file, which can be used to go the official command go build (go build specific parameters are not written, if you need to package other related files into the same exe, there are ICONS, etc.).

If you want to run the.exe file directly, you will need to make a few changes to the xjar.go file packaged by Maven.

A look at the xjar.go file reveals that Java -jar ***.xjar is internally executed directly by calling the CMD command line. The entry is in the main() method, which takes the three arguments passed in(java-jar ***.xjar) and calls subsequent methods.

  1. Let’s take the first parameterjavaPull it out as an order of execution
  2. The following parameters are used asjavaCommand execution parameters if you want to set some example-XmxThese are placed after the first parameter and before the penultimate and second parameters
  3. The penultimate argument will be-jarIf not, an error will be reported
  4. The penultimate argument is executedxjar
// This is the method that generates the jar package name in xjar.go, which directly looks for the next argument to the -jar argument as the jar package name to execute
// find jar name from args
func JAR(args []string) (string, error) {
	var jar string

	l := len(args)
	for i := 1; i < l- 1; i++ {
		arg := args[i]
		if arg == "-jar" {
			jar = args[i+1]}}if jar == "" {
		return "", errors.New("unspecified jar name")}return jar, nil
}
Copy the code

Because the default arguments are passed by themselves (see xjar.go java-jar ***.xjar), in order to have this effect by executing xjar.go directly, modify the generated xjar.go file directly

At the entrance to main(), execute the command directly and manually assign the args[] passed in by main()

  1. Assign args[0] to Java
  2. Subsequent values are assigned as startup parameters
  3. The last two are-jar***.xjar

Run xjar.go to start the jar directly, but you’ll find a CMD window to do it, so hide it

Will xjar.goChange the command packaged as exego build  -ldflags="-H windowsgui"
// Add the following line of code under this line
cmd := exec.Command(java, args...)
// Add code
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
Copy the code

This implementation runs the packaged xjar.exe to hide the popover and start the JAR package directly

The last step is to use inno Setup to package the installation. Select xjar. Exe as the execution file and modify the name of the file. Xjar packages are also packaged with other required folders, so encrypted Jar packages are not needed. Specific packaging method is good directly Baidu.