Eclipse - JAR创建失败“Classpath找不到或不可访问的类文件” [英] Eclipse - JAR creation failed "Class files on classpath not found or not accessible for..."

查看:273
本文介绍了Eclipse - JAR创建失败“Classpath找不到或不可访问的类文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import java.lang.reflect。*; 

public class osxhandler implements InvocationHandler {

protected Object targetObject;
protected方法targetMethod;
protected String proxySignature;

static Object macOSXApplication;

//传递此方法一个对象和方法配置执行应用程序关闭逻辑
//传递的方法应该返回一个布尔值,表示退出是否应该发生
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));
}
//如果我们正在设置一个处理程序,通过调用
// com.apple.eawt.Application反映出
尝试关闭菜单项{
方法enableAboutMethod = macOSXApplication.getClass()。getDeclaredMethod(setEnabledAboutMenu,new Class [] {boolean.class});
enableAboutMethod.invoke(macOSXApplication,new Object [] {Boolean.valueOf(enableAboutMenu)});
} catch(Exception ex){
System.err.println(MacOSHandler无法访问关于菜单);
ex.printStackTrace();
}
}

public static void setPreferencesHandler(Object target,Method prefsHandler){
boolean enablePrefsMenu =(target!= null&& prefsHandler!= null );
if(enablePrefsMenu){
setHandler(new HOsx(handlePreferences,target,prefsHandler));
}
//如果我们设置一个处理程序,通过调用
// com.apple.eawt.Application反映
尝试{
方法enablePrefsMethod = macOSXApplication.getClass()。getDeclaredMethod(setEnabledPreferencesMenu,new Class [] {boolean.class});
enablePrefsMethod.invoke(macOSXApplication,new Object [] {Boolean.valueOf(enablePrefsMenu)});
} catch(Exception ex){
System.err.println(MacOSHandler无法访问关于菜单);
ex.printStackTrace();
}
}

//传递此方法一个对象和一个方法,用于处理来自Finder的文档事件
//文档通过
中的CFBundleDocumentTypes字典//应用程序包的Info.plist
public static void setFileHandler(Object target,Method fileHandler){
setHandler(new HOsx(handleOpenFile,target,fileHandler){
//覆盖MacOSHandler.callTarget以发送
//要打开的文件的信息
public boolean callTarget(Object appleEvent){
if(appleEvent!= null){
try {
方法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从传递的MacOSHandler创建一个Proxy对象,并将其添加为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);
}
类applicationListenerClass = Class.forName(com.apple.eawt.ApplicationListener);
方法addListenerMethod = applicationClass.getDeclaredMethod(addApplicationListener,new Class [] {applicationListenerClass});
//在这个处理程序周围创建一个代理对象,可以反映性地添加为Apple ApplicationListener
对象MacOSHandlerProxy = Proxy.newProxyInstance(HOsx.class.getClassLoader(),new Class [] {applicationListenerClass},adapter );
addListenerMethod.invoke(macOSXApplication,new Object [] {MacOSHandlerProxy});
} catch(ClassNotFoundException cnfe){
System.err.println(此版本的Mac OS X不支持Apple EAWT。ApplicationEvent处理已禁用(+ cnfe +));
} catch(Exception ex){//可能是NoSuchMethodException或者IllegalAccessException加载/调用eawt.Application方法
System.err.println(Mac OS X Adapter can not talk to EAWT:);
ex.printStackTrace();
}
}

//每个MacOSHandler都有其打算侦听的EAWT方法的名称(例如,handleAbout),
//对象将最终执行任务,并且该方法被调用该对象
受保护的HOsx(String proxySignature,Object target,Method handler){
this.proxySignature = proxySignature;
this.targetObject = target;
this.targetMethod = handler;
}

//覆盖此方法对事件执行任何操作
//随各种回调
//参见上面的setFileHandler一个例子
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实现
//这是我们的代理对象的入口点;每次调用ApplicationListener方法时调用它
public Object invoke(Object proxy,Method method,Object [] args)throws Throwable {
if(isCorrectMethod(method,args)){
boolean handling = callTarget(args [0]);
setApplicationEventHandled(args [0],处理);
}
//所有的ApplicationListener方法都是void;不管发生什么,返回null
返回null;
}

//当创建MacOSHandler实例时,比较调用的方法到目标方法
//(例如handleAbout,handleQuit,handleOpenFile等)
protected boolean isCorrectMethod(Method method,Object [] args){
return(targetMethod!= null&& proxySignature.equals(method.getName())&& args.length == 1) ;
}

//将ApplicationEvent标记为已处理并取消默认行为非常重要
//此方法从代理方法检查布尔值结果并设置事件因此
protected void setApplicationEventHandled(Object event,boolean processed){
if(event!= null){
try {
方法setHandledMethod = event.getClass()。getDeclaredMethod( setHandled,新的Class [] {boolean.class});
//如果目标方法返回一个布尔值,则将其用作提示
setHandledMethod.invoke(event,new Object [] {Boolean.valueOf(processed)});
} catch(Exception ex){
System.err.println(MacOSHandler无法处理ApplicationEvent:+事件);
ex.printStackTrace();
}
}
}
}

任何想法为什么我不能导出/编译?

解决方案

只需对项目进行干净和/或重建。



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


I have a project in Eclipse which has a red cross on it and will not export to a runnable JAR. I can't remember if I have looked at it since I reinstalled Windows on my laptop, but I know that I haven't changed any code. There are no errors in any of the classes, however the error I get points to the following class which deals with the menu items on 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();
                }
            }
        }    
}

Any ideas as to why I can't export/compile? I've never had this issue before

解决方案

Just do a clean and/or rebuild on the project.

You can find it under the Project menu of Eclipse.

这篇关于Eclipse - JAR创建失败“Classpath找不到或不可访问的类文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆