NetBeans开发Java程序在非开发机报NoClassDefFoundError: org/jdom2/JDOMException求助
java.lang.NoClassDefFoundError: org/jdom2/JDOMException in NetBeans-built Java Apps Hey there, let's break down why your app works on your dev machine but throws that JDOM error on a non-dev Windows 10 PC. The core issue here is that your non-dev machine doesn't have access to the JDOM and Xerces libraries your app needs—even though you set them up in NetBeans, they might not be getting packaged or referenced correctly when you deploy the app.
Here are the key fixes to try, step by step:
1. Check Your Library Scope in NetBeans
First, make sure NetBeans is treating those JARs as runtime dependencies, not just compile-time ones:
- Right-click your project in the Projects pane → Properties → Libraries
- Find your custom JDOM library (the one with jdom-2.0.6.jar, xercesImpl.jar, xml-apis.jar)
- For each JAR in the library, check the Scope column. It should be set to Compile (this ensures the JARs are included in both compile and runtime environments). If it's set to Provided, NetBeans won't package them with your app, which is exactly why the non-dev machine can't find the classes.
2. Configure NetBeans to Package Dependent Libraries
NetBeans has a built-in setting to copy all your app's dependencies into a lib folder alongside your main JAR. This is the easiest way to ensure non-dev machines have access to the required JARs:
- Go back to your project's Properties → Build → Packaging
- Check the box labeled Copy Dependent Libraries
- Click OK, then run Clean and Build on your project (right-click project → Clean and Build)
After this, your dist folder (where the compiled JAR lives) will have two things:
- Your main app JAR (e.g.,
YourApp.jar) - A
libfolder containing jdom-2.0.6.jar, xercesImpl.jar, and xml-apis.jar
3. Deploy Both the JAR and lib Folder to the Non-Dev Machine
When moving the app to the other PC, don't just copy the main JAR—copy the entire contents of the dist folder (the JAR and the lib folder). NetBeans automatically updates the app's MANIFEST.MF file to point to the lib folder, so when you double-click the JAR or run it via command line, Java will find all the required libraries.
If you prefer to run it via command line, use this (replace placeholders with your actual app details):
java -jar YourApp.jar
The manifest's Class-Path entry will handle loading the libraries from the lib folder automatically.
4. Verify the Manifest File (If Needed)
If the error still happens, double-check the manifest to ensure it's correctly referencing the libraries:
- Rename your main JAR to
.zipand open it, or use a tool like 7-Zip to browse its contents - Navigate to
META-INF/MANIFEST.MFand open it - Look for a line like this (the order doesn't matter as long as all JARs are listed):
Class-Path: lib/jdom-2.0.6.jar lib/xercesImpl.jar lib/xml-apis.jar
If this line is missing or incorrect, edit the manifest template in NetBeans:
- Go to project Properties → Build → Packaging
- Under Manifest File, click Edit and add the
Class-Pathline manually, then rebuild.
Why This Works
On your dev machine, NetBeans adds the libraries to the runtime classpath automatically, so the app runs fine. But on a non-dev machine, those libraries aren't present unless you explicitly package them with your app. The Copy Dependent Libraries setting takes care of this by bundling everything your app needs in one deployable folder.
内容的提问来源于stack exchange,提问作者Cruxial

