Eclipse RCP应用Maven Tycho构建后报OS.GetForegroundWindow()方法不存在错误
java.lang.NoSuchMethodError for org.eclipse.swt.internal.win32.OS.GetForegroundWindow() in Tycho-built RCP Apps Let’s break down why this happens and walk through actionable fixes:
Why This Error Pops Up
First, a critical note: org.eclipse.swt.internal.win32.OS is part of SWT’s internal, unsupported API. These internal classes/methods aren’t meant for public use—SWT’s team can rename, remove, or modify them between versions without warning. The root issue here is a version mismatch: your local development environment uses a SWT version where GetForegroundWindow() exists, but Tycho is pulling in a different SWT version during the build where this method is missing or altered.
Solutions to Try
Ditch SWT’s internal API (best long-term fix)
Stop relying on unstable internal code. If you need to get the system’s foreground window, use JNA (Java Native Access) to call the Windows API directly—it’s designed for this kind of native interaction and avoids SWT dependency headaches. Here’s a quick example:import com.sun.jna.Native; import com.sun.jna.platform.win32.User32; public class SystemWindowHelper { public static long getForegroundWindowHandle() { User32 user32 = User32.INSTANCE; return Native.getPointerAddress(user32.GetForegroundWindow()); } }Just add JNA (
jnaandjna-platform) as dependencies in your Tycho build configuration.Align SWT versions between dev and build
Make sure the SWT version in your local Eclipse setup matches exactly what Tycho uses:- In your target definition file (
.target), explicitly pin the version of the Windows x86_64 SWT bundle (e.g.,org.eclipse.swt.win32.win32.x86_64;bundle-version="4.26.0"). - Update your MANIFEST.MF or pom.xml to reference this exact version. For MANIFEST.MF:
Require-Bundle: org.eclipse.swt.win32.win32.x86_64;bundle-version="4.26.0"
This eliminates the version mismatch that causes missing methods.
- In your target definition file (
Validate your Tycho target platform
Double-check that your build is using the correct target platform. Sometimes Tycho pulls in a default SWT version from a p2 repository that differs from your dev environment. Open your target definition, confirm all SWT-related bundles are included, and set it as the active target for your build.Ensure native libraries are packaged correctly
SWT relies on Windows.dllfiles to work. Verify Tycho is including these in your build output:- Open your product configuration (
.product) and check that the SWT bundle is listed in the "Plug-ins and Fragments" section. - After building, look in the output directory’s
plugins/org.eclipse.swt.win32.win32.x86_64.*folder—you should see the required.dllfiles there.
- Open your product configuration (
Force consistent SWT dependencies (if you must keep the internal method)
If you absolutely can’t switch away fromOS.GetForegroundWindow(), lock your build to the exact SWT jar you use locally. You can install the jar into a local Maven repository and reference it in your pom.xml, or use a p2 repository that hosts your specific SWT version.
内容的提问来源于stack exchange,提问作者Subash J

