Eclipse中Swing无法访问sun.java2d.loops类,项目运行失败求助
Hey there! Let's break down why your project works smoothly in IntelliJ but throws errors in Eclipse—after looking at your paintComponent code, the root cause is almost certainly resource loading differences between the two IDEs and how they handle file paths. Here's what to fix:
1. Ditch File System Paths for Classpath Resource Loading
Your current code uses new File(...) to load images, which relies on the working directory of the running app. IntelliJ and Eclipse use different default working directories:
- IntelliJ typically runs from the project root folder
- Eclipse often runs from the project's
binoutput folder
This means paths like ./res/images/tresors/... point to different locations in each IDE, leading to the IOException your friend is seeing. Instead, load resources from the classpath using the class loader—it's IDE-agnostic and works when you package the project as a JAR too.
2. Rewrite Your Image Loading Code
Here's how to adjust your code to use classpath resources (adjust paths to match your actual resource structure):
protected void paintComponent(Graphics g) { super.paintComponent(g); try { // Load main image from classpath URL mainImageUrl = getClass().getResource("/" + image); if (mainImageUrl == null) { throw new FileNotFoundException("Main image not found: " + image); } BufferedImage bImage = ImageIO.read(mainImageUrl); g.drawImage(bImage, 2, 2, (int) getSize().getWidth(), (int) getSize().getHeight(), this); // Load player images for(int k=0;k<joueurs.size();k++) { URL playerImageUrl = getClass().getResource("/" + Graphiques.Pions[joueurs.get(k).getId()]); if (playerImageUrl == null) { throw new FileNotFoundException("Player image not found: " + Graphiques.Pions[joueurs.get(k).getId()]); } BufferedImage jImage = ImageIO.read(playerImageUrl); System.out.println("Affichage des joueurs X:"+32*(int)(k%2) +" Y:"+ (getSize().getHeight()/2-40*(k/2))); g.drawImage(jImage,32*(k%2), (int) getSize().getHeight()/2-40*(int)(k/2),28,40, this); } // Load artifact image (fixed path syntax) if(artefact!=null){ String artifactPath = "/images/tresors/" + artefact.getElementArtefact() + ".png"; URL artifactImageUrl = getClass().getResource(artifactPath); if (artifactImageUrl == null) { throw new FileNotFoundException("Artifact image not found: " + artifactPath); } BufferedImage jImage = ImageIO.read(artifactImageUrl); g.drawImage(jImage,60, 8,28,40, this); } // Border logic remains unchanged if(zone!=null){ switch (zone.getTypeSelection()){ case 0: setBorder(Graphiques.INACTIVE_BORDER); break; case 1: setBorder(Graphiques.ACTIVE_BORDER_SELECTED); break; case 2: setBorder(Graphiques.ACTIVE_BORDER_SHORE_HOVER); break; } } } catch (IOException e) { e.printStackTrace(); System.err.println("Debug: Failed to load image - " + e.getMessage()); } }
3. Configure Eclipse to Recognize Your Resource Folder
In Eclipse, you need to mark your res folder as a Source Folder so it copies resources to the bin directory during compilation:
- Right-click your
resfolder in the Package Explorer - Select Build Path > Use as Source Folder
4. Fix Path Syntax Errors
Note that in your original artifact path, you used "\.png"—that's an invalid escape sequence in Java. Use "/" (cross-platform compatible) or File.separator instead of \ for path separators.
Once you make these changes, your friend should be able to run the project in Eclipse without issues!
内容的提问来源于stack exchange,提问作者DJAMEL DAHMANE

