This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new f2a39e50e3af CAMEL-26944: camel-jbang - the Windows scripts run
findstr, powershell, curl and java from the system, not from the current
directory (#29885)
f2a39e50e3af is described below
commit f2a39e50e3af055b2245c9d1dc41378fc54d6dbf
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 14 08:50:58 2026 +0100
CAMEL-26944: camel-jbang - the Windows scripts run findstr, powershell,
curl and java from the system, not from the current directory (#29885)
cmd looks for a bare command name in the current directory before PATH, so
a findstr.bat, powershell.bat, curl.bat or java.bat in the directory where
camel runs ran instead of the real program.
- camel.bat runs %SystemRoot%\System32\findstr.exe.
- camelw.cmd runs PowerShell (now with -NoProfile -NonInteractive) and curl
from %SystemRoot%\System32, and without JAVA_HOME takes java.exe from PATH
only, with a clear error when there is none.
- camelw.cmd no longer reports a failed download and exits 1 when
PowerShell failed and curl then succeeded.
- New Windows-only tests (CamelBatDiscoveryTest, CamelwCmdTest) in the
windows-validator job of package-native-validation.yml.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.github/workflows/package-native-validation.yml | 4 +-
.../src/main/resources/camel-wrapper/camelw.cmd | 33 +++--
.../src/main/resources/bin/camel.bat | 10 +-
.../dsl/jbang/launcher/CamelBatDiscoveryTest.java | 25 ++++
.../camel/dsl/jbang/launcher/CamelwCmdTest.java | 157 +++++++++++++++++++++
.../apache/camel/dsl/jbang/launcher/FakeJava.java | 11 ++
6 files changed, 225 insertions(+), 15 deletions(-)
diff --git a/.github/workflows/package-native-validation.yml
b/.github/workflows/package-native-validation.yml
index 68e65a273c24..bfa9a1723096 100644
--- a/.github/workflows/package-native-validation.yml
+++ b/.github/workflows/package-native-validation.yml
@@ -23,6 +23,7 @@ on:
- main
paths:
- 'dsl/camel-jbang/camel-launcher/**'
+ -
'dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd'
- 'tooling/camel-exe/**'
- '.github/workflows/package-native-validation.yml'
pull_request:
@@ -30,6 +31,7 @@ on:
- main
paths:
- 'dsl/camel-jbang/camel-launcher/**'
+ -
'dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd'
- 'tooling/camel-exe/**'
- '.github/workflows/package-native-validation.yml'
workflow_dispatch:
@@ -222,7 +224,7 @@ jobs:
# surefire fork timeout, so slow-but-passing runs were killed. Give
this job's fork more room.
run: |
cd dsl/camel-jbang/camel-launcher
- ../../../mvnw -B -P apache-snapshots -ntp test -pl .
-Dtest=WebsiteInstallTest,CamelBatDiscoveryTest,FakeJavaTest
-Dcamel.surefire.forkTimeout=2700
+ ../../../mvnw -B -P apache-snapshots -ntp test -pl .
-Dtest=WebsiteInstallTest,CamelBatDiscoveryTest,CamelwCmdTest,FakeJavaTest
-Dcamel.surefire.forkTimeout=2700
# Per-test timings for the PowerShell suite, which runs close to
camel-launcher's surefire fork timeout.
# A fork killed on that timeout writes no XML report; the per-process
timings the fixture prints to the
diff --git
a/dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd
b/dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd
index fdb5822d0b2e..7b434908301b 100644
---
a/dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd
+++
b/dsl/camel-jbang/camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd
@@ -58,7 +58,15 @@ if "%DISTRIBUTION_URL%"=="" (
@REM Find Java
if not "%JAVA_HOME%"=="" goto javaHomeSet
-set "JAVACMD=java"
+@REM java.exe from PATH only: cmd looks for a bare "java" in the current
directory before PATH
+set "JAVACMD="
+for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i"
+if "%JAVACMD%"=="" (
+ echo Error: JAVA_HOME is not set and no java.exe was found on PATH. >&2
+ echo Please set the JAVA_HOME variable in your environment to match the >&2
+ echo location of your Java installation. >&2
+ exit /b 1
+)
goto javaFound
:javaHomeSet
@@ -81,18 +89,21 @@ if exist "%CAMEL_LAUNCHER_JAR%" goto runCamel
if not exist "%CAMEL_CACHE_DIR%" mkdir "%CAMEL_CACHE_DIR%"
echo Downloading Camel Launcher %CAMEL_VERSION%...
+@REM PowerShell and curl by their full path: cmd looks for a bare name in the
current directory before PATH, so a
+@REM powershell.bat or curl.bat in the project would run instead.
@REM Try PowerShell download
-powershell -Command "& { [Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%DISTRIBUTION_URL%'
-OutFile '%CAMEL_LAUNCHER_JAR%' }" >nul 2>&1
-if %ERRORLEVEL% neq 0 (
- @REM Try curl as fallback
- curl -fsSL -o "%CAMEL_LAUNCHER_JAR%" "%DISTRIBUTION_URL%" >nul 2>&1
- if %ERRORLEVEL% neq 0 (
- echo Error: Failed to download Camel Launcher from %DISTRIBUTION_URL%
>&2
- del /f "%CAMEL_LAUNCHER_JAR%" >nul 2>&1
- exit /b 1
- )
-)
+"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile
-NonInteractive -Command "& { [Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%DISTRIBUTION_URL%'
-OutFile '%CAMEL_LAUNCHER_JAR%' }" >nul 2>&1
+if not errorlevel 1 goto downloaded
+
+@REM Try curl as fallback (part of Windows since Windows 10 version 1803)
+"%SystemRoot%\System32\curl.exe" -fsSL -o "%CAMEL_LAUNCHER_JAR%"
"%DISTRIBUTION_URL%" >nul 2>&1
+if not errorlevel 1 goto downloaded
+
+echo Error: Failed to download Camel Launcher from %DISTRIBUTION_URL% >&2
+del /f "%CAMEL_LAUNCHER_JAR%" >nul 2>&1
+exit /b 1
+:downloaded
echo Camel Launcher %CAMEL_VERSION% downloaded successfully.
:runCamel
diff --git a/dsl/camel-jbang/camel-launcher/src/main/resources/bin/camel.bat
b/dsl/camel-jbang/camel-launcher/src/main/resources/bin/camel.bat
index 909cad68121d..9defc6d8caf0 100644
--- a/dsl/camel-jbang/camel-launcher/src/main/resources/bin/camel.bat
+++ b/dsl/camel-jbang/camel-launcher/src/main/resources/bin/camel.bat
@@ -23,6 +23,10 @@
@REM Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
+@REM Tools of Windows by their full path: cmd looks for a bare name in the
current directory before PATH, so a
+@REM findstr.bat in the directory where camel runs would run instead.
+set "_FINDSTR=%SystemRoot%\System32\findstr.exe"
+
@REM Find the project base dir
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
@@ -53,7 +57,7 @@ if "%JAVA_OPTS%" == "" set JAVA_OPTS=-Xmx512m
@REM other JARs in lib\).
set "LAUNCHER_JAR="
for %%i in ("%BASEDIR%\..\lib\camel-launcher-*.jar") do (
- echo %%~nxi| findstr /i /e /l /c:"-sources.jar" /c:"-javadoc.jar" > nul
+ echo %%~nxi| %_FINDSTR% /i /e /l /c:"-sources.jar" /c:"-javadoc.jar" > nul
if errorlevel 1 if not defined LAUNCHER_JAR set "LAUNCHER_JAR=%%~fi"
)
@@ -132,7 +136,7 @@ if not exist "%_CAND%" exit /b 1
@REM The release file of the JDK (bin\..\release) has the version, which saves
starting a JVM for -version.
set "_RAW="
for %%d in ("%_CAND%") do set "_RELEASE=%%~dpd..\release"
-if exist "%_RELEASE%" for /f "tokens=2 delims==" %%v in ('findstr /b /l
/c:"JAVA_VERSION=" "%_RELEASE%"') do if not defined _RAW set "_RAW=%%v"
+if exist "%_RELEASE%" for /f "tokens=2 delims==" %%v in ('%_FINDSTR% /b /l
/c:"JAVA_VERSION=" "%_RELEASE%"') do if not defined _RAW set "_RAW=%%v"
set "_RELEASE="
if defined _RAW goto tryJavaParseVersion
for %%e in ("%_CAND%") do set "_CAND_EXT=%%~xe"
@@ -165,7 +169,7 @@ exit /b 1
:tryJavaParseProbe
set "_RAW="
-for /f "tokens=3" %%v in ('findstr /b /l /i /c:"java version " /c:"openjdk
version " "%_PROBE%"') do if not defined _RAW set "_RAW=%%v"
+for /f "tokens=3" %%v in ('%_FINDSTR% /b /l /i /c:"java version " /c:"openjdk
version " "%_PROBE%"') do if not defined _RAW set "_RAW=%%v"
del /q "%_PROBE%" >NUL 2>&1
set "_PROBE="
if not defined _RAW exit /b 1
diff --git
a/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelBatDiscoveryTest.java
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelBatDiscoveryTest.java
index ae1ee1b11144..3f79d4e7cf12 100644
---
a/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelBatDiscoveryTest.java
+++
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelBatDiscoveryTest.java
@@ -322,6 +322,31 @@ class CamelBatDiscoveryTest {
assertTrue(r.stdout().contains("RAN=RELEASE"), r.stdout());
}
+ @Test
+ void runsTheFindstrOfWindowsNotOneInTheCurrentDirectory(@TempDir Path
base) throws Exception {
+ // cmd looks for a bare command name in the current directory before
PATH: a findstr.bat in the directory
+ // where camel runs (a cloned project) must not run. camel.bat runs
findstr for the launcher JAR, the release
+ // file of the JDK and the banner of -version.
+ Path project = Files.createDirectories(base.resolve("project"));
+ Files.writeString(project.resolve("findstr.bat"),
+ "@echo off\r\necho planted> \"%~dp0planted.txt\"\r\nexit /b
0\r\n");
+ Path h = home(base);
+ Path probed = FakeJava.writeFakeJava(base.resolve("probed"), "java",
JAVA21, 0, "PROBED");
+ Path released =
FakeJava.writeFakeJava(base.resolve("jdk").resolve("bin"), "java", GARBAGE, 0,
"RELEASE");
+ Files.writeString(base.resolve("jdk").resolve("release"),
"JAVA_VERSION=\"21.0.3\"\r\n");
+
+ for (Path java : List.of(probed, released)) {
+ Map<String, String> env = isolatedEnvironment();
+ env.put("JAVACMD", java.toString());
+
+ FakeJava.Result r = FakeJava.runIn(project,
h.resolve("camel.bat"), env, null, "version");
+
+ assertEquals(0, r.exitCode(), r.stderr());
+ assertTrue(r.stdout().contains("RAN=" + (java == probed ? "PROBED"
: "RELEASE")), r.stdout());
+ assertFalse(Files.exists(project.resolve("planted.txt")), "the
findstr.bat of the current directory ran");
+ }
+ }
+
@Test
void rejectsAnOldJavaFromTheReleaseFileOfTheJdk(@TempDir Path base) throws
Exception {
Path h = home(base);
diff --git
a/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelwCmdTest.java
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelwCmdTest.java
new file mode 100644
index 000000000000..ef575fe63f5f
--- /dev/null
+++
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/CamelwCmdTest.java
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.launcher;
+
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The Windows script of the Camel wrapper (camelw.cmd, which {@code camel
wrapper} writes into a project) runs java,
+ * PowerShell and curl of the system, never a java.bat, powershell.bat or
curl.bat in the project: cmd looks for a bare
+ * command name in the current directory before PATH.
+ */
+@EnabledOnOs(OS.WINDOWS)
+class CamelwCmdTest {
+
+ private static final Path SCRIPT =
Paths.get("../camel-jbang-run/src/main/resources/camel-wrapper/camelw.cmd");
+ private static final String VERSION = "9.9.9";
+
+ @Test
+ void runsTheJavaOfPathNotOneInTheProject(@TempDir Path base) throws
Exception {
+ Path project = project(base, "file:///not-used");
+ plant(project, "java");
+ Path user = Files.createDirectories(base.resolve("user"));
+ writeMarkerJar(cachedJar(user));
+ Map<String, String> env = environment(user);
+ env.put("PATH", currentJavaHome().resolve("bin") + ";" +
env.get("PATH"));
+
+ FakeJava.Result r = FakeJava.runIn(project,
project.resolve("camelw.cmd"), env, null, "hello");
+
+ assertEquals(0, r.exitCode(), r.stderr());
+ assertTrue(r.stdout().contains("RAN=NATIVE-JAVA"), r.stdout());
+ assertTrue(r.stdout().contains("ARGS=[hello]"), r.stdout());
+ assertFalse(Files.exists(project.resolve("planted-java.txt")), "the
java.bat of the project ran");
+ }
+
+ @Test
+ void failsWhenNoJavaIsOnPath(@TempDir Path base) throws Exception {
+ Path project = project(base, "file:///not-used");
+ plant(project, "java");
+ Path user = Files.createDirectories(base.resolve("user"));
+ writeMarkerJar(cachedJar(user));
+
+ FakeJava.Result r = FakeJava.runIn(project,
project.resolve("camelw.cmd"), environment(user), null, "hello");
+
+ assertEquals(1, r.exitCode(), r.stdout());
+ assertTrue(r.stderr().contains("no java.exe was found on PATH"),
r.stderr());
+ assertFalse(Files.exists(project.resolve("planted-java.txt")), "the
java.bat of the project ran");
+ }
+
+ @Test
+ void downloadsWithThePowerShellOrCurlOfWindows(@TempDir Path base) throws
Exception {
+ Path distribution =
Files.createDirectories(base.resolve("distribution")).resolve("camel-launcher.jar");
+ writeMarkerJar(distribution);
+ Path project = project(base, distribution.toUri().toString());
+ plant(project, "powershell");
+ plant(project, "curl");
+ Path user = Files.createDirectories(base.resolve("user"));
+ Map<String, String> env = environment(user);
+ env.put("PATH", currentJavaHome().resolve("bin") + ";" +
env.get("PATH"));
+
+ FakeJava.Result r = FakeJava.runIn(project,
project.resolve("camelw.cmd"), env, null, "hello");
+
+ assertEquals(0, r.exitCode(), r.stdout() + r.stderr());
+ assertTrue(r.stdout().contains("downloaded successfully"), r.stdout());
+ assertTrue(r.stdout().contains("RAN=NATIVE-JAVA"), r.stdout());
+ assertTrue(Files.isRegularFile(cachedJar(user)), "the launcher JAR is
not in the cache of the wrapper");
+ assertFalse(Files.exists(project.resolve("planted-powershell.txt")),
"the powershell.bat of the project ran");
+ assertFalse(Files.exists(project.resolve("planted-curl.txt")), "the
curl.bat of the project ran");
+ }
+
+ private static Path project(Path base, String distributionUrl) throws
Exception {
+ Path project = Files.createDirectories(base.resolve("project"));
+ Files.copy(SCRIPT, project.resolve("camelw.cmd"));
+
Files.writeString(Files.createDirectories(project.resolve(".camel")).resolve("camel-wrapper.properties"),
+ "camel.version=" + VERSION + "\r\ndistributionUrl=" +
distributionUrl + "\r\n");
+ return project;
+ }
+
+ /**
+ * A batch file in the project with the name of a tool, which leaves a
file behind when it runs.
+ */
+ private static void plant(Path project, String tool) throws Exception {
+ Files.writeString(project.resolve(tool + ".bat"),
+ "@echo off\r\necho planted> \"%~dp0planted-" + tool +
".txt\"\r\nexit /b 0\r\n");
+ }
+
+ private static Path cachedJar(Path user) throws Exception {
+ return
Files.createDirectories(user.resolve(".camel").resolve("wrapper"))
+ .resolve("camel-launcher-" + VERSION + ".jar");
+ }
+
+ private static Map<String, String> environment(Path user) {
+ String systemRoot = System.getenv("SystemRoot");
+ assertTrue(systemRoot != null && !systemRoot.isBlank(), "Windows test
fixture requires SystemRoot");
+ Map<String, String> env = new HashMap<>();
+ env.put("PATH", Path.of(systemRoot, "System32").toString());
+ env.put("USERPROFILE", user.toString());
+ env.put("TEMP", System.getProperty("java.io.tmpdir"));
+ env.put("TMP", System.getProperty("java.io.tmpdir"));
+ return env;
+ }
+
+ private static Path currentJavaHome() {
+ Path javaHome = Path.of(System.getProperty("java.home"));
+ assertTrue(Files.isRegularFile(javaHome.resolve("bin/java.exe")),
+ "Windows test fixture requires the current JDK java.exe: " +
javaHome);
+ return javaHome;
+ }
+
+ /**
+ * A runnable JAR whose main class prints RAN=NATIVE-JAVA and the
arguments (see {@link CamelBatRealJavaMarker}).
+ */
+ private static void writeMarkerJar(Path jar) throws Exception {
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
CamelBatRealJavaMarker.class.getName());
+ String entryName = CamelBatRealJavaMarker.class.getName().replace('.',
'/') + ".class";
+ try (InputStream classBytes =
CamelBatRealJavaMarker.class.getResourceAsStream("/" + entryName);
+ JarOutputStream out = new
JarOutputStream(Files.newOutputStream(jar), manifest)) {
+ assertTrue(classBytes != null, "could not load marker class
bytes");
+ out.putNextEntry(new JarEntry(entryName));
+ classBytes.transferTo(out);
+ out.closeEntry();
+ }
+ }
+}
diff --git
a/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/FakeJava.java
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/FakeJava.java
index 97cd951a50b9..ebedcc35c331 100644
---
a/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/FakeJava.java
+++
b/dsl/camel-jbang/camel-launcher/src/test/java/org/apache/camel/dsl/jbang/launcher/FakeJava.java
@@ -116,6 +116,14 @@ final class FakeJava {
}
static Result runWithInput(Path script, Map<String, String> env, String
input, String... args) throws Exception {
+ return runIn(null, script, env, input, args);
+ }
+
+ /**
+ * Runs the script with the given working directory (null for the one of
this JVM).
+ */
+ static Result runIn(Path workDir, Path script, Map<String, String> env,
String input, String... args)
+ throws Exception {
List<String> cmd = new ArrayList<>();
if (WINDOWS) {
cmd.add("cmd.exe");
@@ -138,6 +146,9 @@ final class FakeJava {
pb.environment().put("PATHEXT", ".COM;.EXE;.BAT;.CMD");
}
pb.environment().putAll(env);
+ if (workDir != null) {
+ pb.directory(workDir.toFile());
+ }
Process p = pb.start();
ExecutorService collectors = Executors.newFixedThreadPool(2);
Throwable failure = null;