In the install4j Launcher, I'd like to run a small snippet of custom code (like the Run Script Action) in order to set a CLASSPATH directory conditionally on a number of things (OS, the user launching the application, whether the directory exists), and add that directory to the Class path in the Java invocation of the Launcher.
Specifically, can I set a preferred class path for a Launcher's Java invocation to a folder in user-space, which will depend on the OS and the installation instance (probably something like
%LOCALAPPDATA%\MyApplication\Profiles\hexcodeforthisinstallation
on Windows;
~/Library/Application Support/MyApplication/Profiles/hexcodeforthisinstallation
on macOS;
~/.local/share/MyApplication/profiles/hexcodeforthisinstallation
on everything else)?
This is for a multi-user environment so the installation user can't be used.
The classpath is not set in the Java part of the installer, but in the native part of the launcher, so it's not possible to run Java code to change it.
If you need to construct the classpath dynamically, write a wrapper main class that loads the actual main with a custom classloader.
public class DynamicLoader {
public static void main(String[] args) throws Exception {
File additionalJarFile = ...;
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {additionalJarFile.toURI().toURL()});
Class<?> mainClass= Class.forName("com.example.YourMainClass", true, classLoader);
Object instance = mainClass.getDeclaredConstructor().newInstance();
// Get the method to be invoked
Method mainMethod = loadedClass.getMethod("main", String[].class);
// Invoke the method on the instance of the class
mainMethod.invoke(instance, (Object) args);
}
}