Skip to content

Commit

Permalink
fix: Align the windows java path
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Mar 15, 2024
1 parent 4e734a5 commit 69573f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions xmcl-runtime/java/JavaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { readdirIfPresent } from '../util/fs'
import { requireString } from '../util/object'
import { SafeFile, createSafeFile } from '../util/persistance'
import { ensureClass, getJavaArch } from './detectJVMArch'
import { getMojangJavaPaths, getOpenJdkPaths, getOrcaleJavaPaths } from './javaPaths'

@ExposeServiceKey(JavaServiceKey)
export class JavaService extends StatefulService<JavaState> implements IJavaService {
Expand Down Expand Up @@ -204,9 +205,11 @@ export class JavaService extends StatefulService<JavaState> implements IJavaServ
this.log('Force update or no local cache found. Scan java through the disk.')
const commonLocations = [] as string[]
if (this.app.platform.os === 'windows') {
let files = await readdirIfPresent('C:\\Program Files\\Java')
files = files.map(f => join('C:\\Program Files\\Java', f, 'bin', 'java.exe'))
commonLocations.push(...files)
commonLocations.push(
...await getMojangJavaPaths(),
...await getOrcaleJavaPaths(),
...await getOpenJdkPaths(),
)
}
const javas = await scanLocalJava(commonLocations)
const infos = await Promise.all(javas.map(async (j) => ({ ...j, valid: true, arch: await getJavaArch(this.app, j.path) })))
Expand Down
21 changes: 21 additions & 0 deletions xmcl-runtime/java/javaPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readdir } from 'fs-extra'
import { join } from 'path'

export async function getMojangJavaPaths() {
const runtimeDir = 'C:\\Program Files (x86)\\Minecraft Launcher\\runtime'
const runtimes = await readdir(runtimeDir).catch(() => [])
const arch = process.arch === 'ia32' ? 'x86' : 'x64'
const platformArch = `windows-${arch}`
return runtimes.map((runtime) => join(runtimeDir, runtime, platformArch, runtime, 'bin', 'javaw.exe'))
.flat()
}

export async function getOrcaleJavaPaths() {
const files = await readdir('C:\\Program Files\\Java').catch(() => [])
return files.map(f => join('C:\\Program Files\\Java', f, 'bin', 'javaw.exe'))
}

export async function getOpenJdkPaths() {
const files = await readdir('C:\\Program Files\\AdoptOpenJDK').catch(() => [])
return files.map(f => join('C:\\Program Files\\AdoptOpenJDK', f, 'bin', 'javaw.exe'))
}

0 comments on commit 69573f4

Please sign in to comment.