Eclipse - JAR创建失败“类路径上的类文件未找到或无法访问...”

时间:2013-08-18 15:39:52

标签: java eclipse macos jar

我在Eclipse中有一个项目,它上面有一个红叉,不会导出到可运行的JAR。自从我在笔记本电脑上重新安装Windows以来,我不记得是否看过它,但我知道我没有更改任何代码。任何类都没有错误,但是我得到的错误指向下面的类,它处理Mac OSx上的菜单项:

import java.lang.reflect.*;

public class osxhandler implements InvocationHandler {

      protected Object targetObject;
        protected Method targetMethod;
        protected String proxySignature;

        static Object macOSXApplication;

        // Pass this method an Object and Method equipped to perform application shutdown logic
        // The method passed should return a boolean stating whether or not the quit should occur
        public static void setQuitHandler(Object target, Method quitHandler) {
            setHandler(new HOsx("handleQuit", target, quitHandler));
        }


    public static void setAboutHandler(Object target, Method aboutHandler) {
        boolean enableAboutMenu = (target != null && aboutHandler != null);
        if (enableAboutMenu) {
            setHandler(new HOsx("handleAbout", target, aboutHandler));
        }
        // If we're setting a handler, enable the About menu item by calling
        // com.apple.eawt.Application reflectively
        try {
            Method enableAboutMethod = macOSXApplication.getClass().getDeclaredMethod("setEnabledAboutMenu", new Class[] { boolean.class });
            enableAboutMethod.invoke(macOSXApplication, new Object[] { Boolean.valueOf(enableAboutMenu) });
        } catch (Exception ex) {
            System.err.println("MacOSHandler could not access the About Menu");
            ex.printStackTrace();
        }
    }

       public static void setPreferencesHandler(Object target, Method prefsHandler) {
            boolean enablePrefsMenu = (target != null && prefsHandler != null);
            if (enablePrefsMenu) {
                setHandler(new HOsx("handlePreferences", target, prefsHandler));
            }
            // If we're setting a handler, enable the Preferences menu item by calling
            // com.apple.eawt.Application reflectively
            try {
                Method enablePrefsMethod = macOSXApplication.getClass().getDeclaredMethod("setEnabledPreferencesMenu", new Class[] { boolean.class });
                enablePrefsMethod.invoke(macOSXApplication, new Object[] { Boolean.valueOf(enablePrefsMenu) });
            } catch (Exception ex) {
                System.err.println("MacOSHandler could not access the About Menu");
                ex.printStackTrace();
            }
        }

        // Pass this method an Object and a Method equipped to handle document events from the Finder
        // Documents are registered with the Finder via the CFBundleDocumentTypes dictionary in the 
        // application bundle's Info.plist
        public static void setFileHandler(Object target, Method fileHandler) {
            setHandler(new HOsx("handleOpenFile", target, fileHandler) {
                // Override MacOSHandler.callTarget to send information on the
                // file to be opened
                public boolean callTarget(Object appleEvent) {
                    if (appleEvent != null) {
                        try {
                            Method getFilenameMethod = appleEvent.getClass().getDeclaredMethod("getFilename", (Class[])null);
                            String filename = (String) getFilenameMethod.invoke(appleEvent, (Object[])null);
                            this.targetMethod.invoke(this.targetObject, new Object[] { filename });
                        } catch (Exception ex) {

                        }
                    }
                    return true;
                }
            });
        }

        // setHandler creates a Proxy object from the passed MacOSHandler and adds it as an ApplicationListener
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void setHandler(HOsx adapter) {
            try {
                Class applicationClass = Class.forName("com.apple.eawt.Application");
                if (macOSXApplication == null) {
                    macOSXApplication = applicationClass.getConstructor((Class[])null).newInstance((Object[])null);
                }
                Class applicationListenerClass = Class.forName("com.apple.eawt.ApplicationListener");
                Method addListenerMethod = applicationClass.getDeclaredMethod("addApplicationListener", new Class[] { applicationListenerClass });
                // Create a proxy object around this handler that can be reflectively added as an Apple ApplicationListener
                Object MacOSHandlerProxy = Proxy.newProxyInstance(HOsx.class.getClassLoader(), new Class[] { applicationListenerClass }, adapter);
                addListenerMethod.invoke(macOSXApplication, new Object[] { MacOSHandlerProxy });
            } catch (ClassNotFoundException cnfe) {
                System.err.println("This version of Mac OS X does not support the Apple EAWT.  ApplicationEvent handling has been disabled (" + cnfe + ")");
            } catch (Exception ex) {  // Likely a NoSuchMethodException or an IllegalAccessException loading/invoking eawt.Application methods
                System.err.println("Mac OS X Adapter could not talk to EAWT:");
                ex.printStackTrace();
            }
        }

        // Each MacOSHandler has the name of the EAWT method it intends to listen for (handleAbout, for example),
        // the Object that will ultimately perform the task, and the Method to be called on that Object
        protected HOsx(String proxySignature, Object target, Method handler) {
            this.proxySignature = proxySignature;
            this.targetObject = target;
            this.targetMethod = handler;
        }

        // Override this method to perform any operations on the event 
        // that comes with the various callbacks
        // See setFileHandler above for an example
        public boolean callTarget(Object appleEvent) throws InvocationTargetException, IllegalAccessException {
            Object result = targetMethod.invoke(targetObject, (Object[])null);
            if (result == null) {
                return true;
            }
            return Boolean.valueOf(result.toString()).booleanValue();
        }

        // InvocationHandler implementation
        // This is the entry point for our proxy object; it is called every time an ApplicationListener method is invoked
        public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
            if (isCorrectMethod(method, args)) {
                boolean handled = callTarget(args[0]);
                setApplicationEventHandled(args[0], handled);
            }
            // All of the ApplicationListener methods are void; return null regardless of what happens
            return null;
        }

        // Compare the method that was called to the intended method when the MacOSHandler instance was created
        // (e.g. handleAbout, handleQuit, handleOpenFile, etc.)
        protected boolean isCorrectMethod(Method method, Object[] args) {
            return (targetMethod != null && proxySignature.equals(method.getName()) && args.length == 1);
        }

        // It is important to mark the ApplicationEvent as handled and cancel the default behavior
        // This method checks for a boolean result from the proxy method and sets the event accordingly
        protected void setApplicationEventHandled(Object event, boolean handled) {
            if (event != null) {
                try {
                    Method setHandledMethod = event.getClass().getDeclaredMethod("setHandled", new Class[] { boolean.class });
                    // If the target method returns a boolean, use that as a hint
                    setHandledMethod.invoke(event, new Object[] { Boolean.valueOf(handled) });
                } catch (Exception ex) {
                    System.err.println("MacOSHandler was unable to handle an ApplicationEvent: " + event);
                    ex.printStackTrace();
                }
            }
        }    
}

关于为什么我无法导出/编译的任何想法?

之前我从未遇到过这个问题

9 个答案:

答案 0 :(得分:29)

只需对项目进行清理和/或重建。

您可以在Eclipse的Project菜单下找到它。

答案 1 :(得分:3)

我也有一个不同的,退化的问题。事实证明,我们的项目中有一个类有一个文件(所以Eclipse将它保存在类路径中),但文件中没有定义实际的类(文件只有导入和类注释......可能是合并错误) 。无论如何,删除文件解决了问题。

答案 2 :(得分:2)

  

Eclipse总是生成隐藏文件.project是非常可恶的   和项目文件夹中的.classpath。有时你不知道是否   这些文件出了问题。

     

升级Eclipse后,如果发现以下编译   错误,我建议您检查项目文件夹中的.classpath。

     

项目未构建,因为其构建路径不完整。不能   找到java.lang.Object的类文件。修复构建路径然后尝试   建立这个项目

     

很可能你会看到这样一条线。

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/    org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/j2re1.4.2_03"/>
  

愚蠢的Eclipse无缘无故地附加了这个。只需将其删除即可   让它再次运作。 ;)

     

/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/j2re1.4.2_xx

来源:http://hochit.com/2006/07/06/eclipse-upgrading-problem-javalangobject-not-found/

此外,您可以在日食中查看project settings。右键单击您的项目并选择属性。转到Java Build Path,应该有更具体的问题信息。您很可能将JDK设置为新系统中不存在的版本。

如果这也没有帮助,请选择您的项目,然后使用菜单条目Source->Clean Up

答案 3 :(得分:0)

我在这里被提到了,因为我有同样的错误。 我在日食上使用maven。我右键单击repo,选择了build path-&gt; Conifgure build-&gt; Project References并检查了我的repo的项目引用。这对我有用。

答案 4 :(得分:0)

我也遇到了同样的错误。在我的情况下问题是,我曾经通过“用户库”&amp ;;多次将同一个jar放入下次通过同一个项目的“构建路径”。刚从类路径中删除了重复的jar文件。得到了上述错误。

答案 5 :(得分:0)

就我而言,这些类是空的,并且编译器抱怨:

SQLite

要解决这个问题,我要添加一个类声明:

Class files on classpath not found or not accessible for: 'ibDemo/src/com/ib/controller/LocationCode.java'
Class files on classpath not found or not accessible for: 'ibDemo/src/com/ib/controller/PairPanel.java'

public class LocationCode
{

}

答案 6 :(得分:0)

我遇到了同样的错误,在尝试了多项建议之后,没有任何结果。所以我创建了一个新工作区并参考了这个项目。之后,它成功构建并导出了JAR而没有错误。

答案 7 :(得分:0)

不确定这可能是最好的解决方案,但请检查java构建路径。我把它指向了一个错误的位置,因为我面对的是类找不到错误。 修复java构建路径后,问题就解决了。

答案 8 :(得分:0)

我在Eclipse中关闭了所有带有文件的选项卡,这是已解决的问题。