Skip to content

Commit 609ec6e

Browse files
committed
Embed JDK
1 parent 63b251b commit 609ec6e

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

app/build.gradle.kts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
12
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
3+
import org.jetbrains.compose.internal.de.undercouch.gradle.tasks.download.Download
24

35
plugins{
46
id("java")
@@ -7,6 +9,8 @@ plugins{
79
alias(libs.plugins.compose.compiler)
810
alias(libs.plugins.jetbrainsCompose)
911
alias(libs.plugins.serialization)
12+
13+
id("de.undercouch.download") version "5.6.0"
1014
}
1115

1216
group = rootProject.group
@@ -56,6 +60,7 @@ compose.desktop {
5660
jvmArgs(
5761
"-Dapple.awt.application.appearance=system"
5862
)
63+
appResourcesRootDir.set(layout.buildDirectory.dir("resources-bundled"))
5964
}
6065
}
6166
}
@@ -95,3 +100,25 @@ tasks.register<Copy>("addCore"){
95100
tasks.jar { dependsOn("addCore") }
96101
tasks.processResources{ finalizedBy("addCore") }
97102

103+
val os: OperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
104+
val arch: String = System.getProperty("os.arch")
105+
var platform = "linux"
106+
if (os.isWindows) {
107+
platform = "windows"
108+
} else if (os.isMacOsX) {
109+
platform = "mac"
110+
}
111+
tasks.register<Download>("downloadJDK"){
112+
src("https://api.adoptium.net/v3/binary/latest/17/ga/${platform}/${arch}/jdk/hotspot/normal/eclipse?project=jdk")
113+
dest(layout.buildDirectory.file("jdk-${platform}-${arch}.tar.gz"))
114+
overwrite(false)
115+
}
116+
tasks.register<Copy>("unzipJDK"){
117+
val dl = tasks.findByPath("downloadJDK") as Download
118+
dependsOn(dl)
119+
from(tarTree(dl.dest))
120+
into(layout.buildDirectory.dir("resources-bundled/common"))
121+
}
122+
afterEvaluate {
123+
tasks.findByName("prepareAppResources")?.dependsOn("unzipJDK")
124+
}

app/src/processing/app/Platform.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URL;
3131
import java.nio.file.*;
3232
import java.nio.file.attribute.BasicFileAttributes;
33+
import java.nio.file.attribute.PosixFilePermission;
3334
import java.util.*;
3435

3536
import com.sun.jna.platform.FileUtils;
@@ -424,6 +425,19 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
424425
}
425426

426427
static public File getJavaHome() {
428+
var resourcesDir = new File(System.getProperty("compose.application.resources.dir"));
429+
if(resourcesDir.exists()) {
430+
// find the jdk folder starting with jdk-17
431+
var jdkFolder = Arrays.stream(resourcesDir.listFiles((dir, name) -> dir.isDirectory() && name.startsWith("jdk-17")))
432+
.findFirst()
433+
.orElse(null);
434+
processDirectory(jdkFolder.toPath());
435+
if(Platform.isMacOS()){
436+
return new File(jdkFolder, "Contents/Home");
437+
}
438+
return jdkFolder;
439+
}
440+
427441
var home = System.getProperty("java.home");
428442
if(home != null){
429443
if(new File(home, "bin/java").exists()){
@@ -452,6 +466,49 @@ static public File getJavaHome() {
452466
return getContentFile("java");
453467
}
454468

469+
/**
470+
* Depcated method to set permissions for all files in a directory. Hotfix for gradle embedded jdk.
471+
*/
472+
@Deprecated
473+
private static void processDirectory(Path directory) {
474+
try {
475+
Files.walk(directory)
476+
.filter(Files::isRegularFile)
477+
.filter(path -> {
478+
String fileName = path.getFileName().toString();
479+
Path relativePath = directory.relativize(path);
480+
// Check if file is in a bin directory or has no extension
481+
return relativePath.toString().contains("bin") ||
482+
!fileName.contains(".");
483+
})
484+
.forEach(file -> {
485+
try {
486+
makeExecutable(file);
487+
} catch (Exception e) {
488+
System.err.println("Failed to set permissions for " + file + ": " + e.getMessage());
489+
}
490+
});
491+
} catch (Exception e) {
492+
System.err.println("Error walking directory: " + e.getMessage());
493+
}
494+
}
495+
496+
/**
497+
* Depcated method to set permissions for all files in a directory. Hotfix for gradle embedded jdk.
498+
*/
499+
@Deprecated
500+
private static void makeExecutable(Path file) throws Exception {
501+
try {
502+
Set<PosixFilePermission> permissions = new HashSet<>(Files.getPosixFilePermissions(file));
503+
permissions.add(PosixFilePermission.OWNER_EXECUTE);
504+
permissions.add(PosixFilePermission.GROUP_EXECUTE);
505+
permissions.add(PosixFilePermission.OTHERS_EXECUTE);
506+
Files.setPosixFilePermissions(file, permissions);
507+
} catch (UnsupportedOperationException e) {
508+
// Fallback for non-POSIX systems
509+
file.toFile().setExecutable(true, false);
510+
}
511+
}
455512

456513
/** Get the path to the embedded Java executable. */
457514
static public String getJavaPath() {

0 commit comments

Comments
 (0)