簡體   English   中英

Eclipse - JAR 創建失敗“找不到類路徑上的類文件或無法訪問...”

[英]Eclipse - JAR creation failed "Class files on classpath not found or not accessible for..."

我在 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();
                }
            }
        }    
}

關於為什么我不能導出/編譯的任何想法? 我以前從來沒有遇到過這個問題。

只需對項目進行清理和/或重建。

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

對於這個問題,我也有一個不同的、退化的案例。 原來,我們的項目中有一個類,它有一個文件(所以 Eclipse 將它保存在類路徑中)但文件中沒有定義實際的類(文件只有導入和類注釋......可能是合並出錯了) . 無論如何,刪除文件解決了這個問題。

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/

此外,您可以在eclipse中檢查您的project settings 右鍵單擊您的項目並選擇屬性。 轉到Java Build Path,應該有更具體的問題信息。 您很可能將JDK設置為新系統上不存在的版本。

如果這也沒有幫助,請選擇您的項目,然后使用菜單項Source->Clean Up

在我的例子中,類是空的,編譯器抱怨:

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
{

}

public class PairPanel
{

}

我被推薦到這里,因為我有同樣的錯誤。 我在eclipse上使用maven。 我確實右鍵單擊存儲庫,選擇構建路徑->配置構建->項目引用並檢查了我的存儲庫的項目引用。 這對我有用。

我也遇到了同樣的錯誤。 就我而言,問題是,我通過“用戶庫”多次放置同一個 jar,下一次通過同一項目的“構建路徑”放置。 剛剛從類路徑中刪除了重復的 jar 並解決了上述錯誤。

我有同樣的錯誤,在嘗試了多個建議后,沒有任何結果。 所以我創建了一個新的工作區並參考了這個項目。 之后,它成功構建並導出 JAR,沒有錯誤。

不確定這可能是最好的解決方案,但請檢查 java 構建路徑。 我讓它指向一個錯誤的位置,因為我面臨着類未找到錯誤。 修復 java 構建路徑后,問題就解決了。

我來到這里同樣的錯誤。 在我的例子中,沒有任何東西在編譯(構建?)並且 Eclipse 沒有告訴我除了這些神秘的消息之外構建有任何問題。 我最終解壓縮了 jar 文件,發現里面沒有類。 這是因為我在構建路徑中引用的項目沒有構建。 就我而言,該項目在一百萬年內不會編譯,但我可以訪問研發部門的 jar 文件,他們可以並且確實以自己的方式編譯它。 所以我引用了那些 jar 文件。 現在我的類編譯並且錯誤消失了。 我敢肯定我一開始會這樣做,但是“有用”的 Eclipse 建議我參考未構建的項目,所以我同意了這個壞建議!

我在 Eclipse 中關閉了所有帶有文件的選項卡,它已解決問題。

就我而言,我遇到了同樣的問題,我注意到我 mvn clean 並嘗試導出 jar 並最終得到同樣的錯誤。 在安裝 mvn 之后它對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM