2011-08-16 7 views
51

तक पहुंचने का प्रयास किया गया मुझे अपवाद मिल रहा है और मुझे इसका कारण नहीं मिल रहा है।java.lang.IllegalAccessError: विधि

अपवाद मैं मिलता है:

java.lang.IllegalAccessError: tried to access method Connected.getData(Ljava/lang/String;)Ljava/sql/ResultSet; from class B

विधि सार्वजनिक है।

public class B 
{ 
    public void myMethod() 
    { 
    Connected conn = new Connected(); // create a connected class in order to connect to The DB 
    ResultSet rs = null; // create a result set to get the query result 
    rs = conn.getData(sql); // do sql query 
    } 
} 

public class Connected 
{ 
public ResultSet getData(String sql) 
{ 
    ResultSet rs = null; 
    try 
    { 
    prepareConnection(); 
    stmt = conn.createStatement(); 
    stmt.execute(sql); 
    rs = stmt.getResultSet(); 
    } 
    catch (SQLException E) 
     { 
    System.out.println("Content.getData Error"); 
    E.printStackTrace(); 
     } 
return rs; 
} 

मैं अपाचे बिल्ला 5.5.12 उपयोग कर रहा हूँ और जावा 1.6

उत्तर

56

आप लगभग निश्चित रूप से एक आप उम्मीद करने के क्रम में वर्ग का एक अलग संस्करण का उपयोग कर रहे हैं। विशेष रूप से, रनटाइम क्लास आपके द्वारा संकलित किए गए एक से अलग होगा (अन्यथा यह संकलन-समय त्रुटि उत्पन्न कर सकता है) - क्या यह विधि कभीprivate थी? क्या आपके पास कहीं भी आपके सिस्टम पर कक्षाओं/जार के पुराने संस्करण हैं?

IllegalAccessError राज्य के लिए javadocs के रूप में,

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

मैं निश्चित रूप से अपने classpath को देखो और जाँच करें कि क्या यह किसी भी आश्चर्य रखती हूँ।

+3

@yossi: तो आपने इसे कैसे हल किया? सॉस को दोबारा दोहराएं और इसे अपने कक्षा में रखें? अगर आप मूल वर्ग को डिकंपिल्ड करते हैं और इसमें क्या था, तो बस उत्सुक है। – Victor

+0

जब मेरे पास यह था, मैंने फ़ाइल को एक निजी समारोह के साथ सार्वजनिक रूप से सहेजा था और इसलिए संपादक ने .jsp फ़ाइल में सबकुछ ठीक से देखा लेकिन मैंने मुख्य कार्यक्रम को फिर से लॉन्च नहीं किया था, इसलिए उसने वास्तव में नया संकलित नहीं किया था संस्करण। –

+0

मुझे यह त्रुटि सिर्फ एक मैवेन पैकेज से स्थापित हो रही है। मैंने प्रोजेक्ट को अपडेट किया है और इसे साफ़ कर दिया है लेकिन कुछ भी बदल रहा है .. – softwareplay

5

यदि getData संरक्षित है तो इसे सार्वजनिक बनाने का प्रयास करें। समस्या जावा 1.6 में मौजूद हो सकती है और 1.5x

में अनुपस्थित हो सकती है, मुझे आपकी समस्या के लिए यह मिला। Illegal access error

75

ऐसा तब होता है जब एक ही पैकेज में एक वर्ग की पैकेज स्कॉप्ड विधि तक पहुंच होती है लेकिन एक अलग जार और क्लासलोडर में होती है।

This मेरा स्रोत था, लेकिन लिंक अब टूट गया है।

Packages (as in package access) are scoped per ClassLoader.

You state that the parent ClassLoader loads the interface and the child ClassLoader loads the implementation. This won't work because of the ClassLoader-specific nature of package scoping. The interface isn't visible to the implementation class because, even though it's the same package name, they're in different ClassLoaders.

I only skimmed the posts in this thread, but I think you've already discovered that this will work if you declare the interface to be public. It would also work to have both interface and implementation loaded by the same ClassLoader.

Really, if you expect arbitrary folks to implement the interface (which you apparently do if the implementation is being loaded by a different ClassLoader), then you should make the interface public.

The ClassLoader-scoping of package scope (which applies to accessing package methods, variables, etc.) is similar to the general ClassLoader-scoping of class names. For example, I can define two classes, both named com.foo.Bar, with entirely different implementation code if I define them in separate ClassLoaders.

Joel

+1

मुझे यह समस्या स्ट्रोक 1.1 के साथ जेबॉस ईएपी 5.3 के तहत 'कॉमन्स-संग्रह-3.2.1.jar' पर मिली है क्योंकि यह जेएआर पहले से ही जेबॉस में है पुस्तकालयों लेकिन मेरे अनुप्रयोगों में भी पैक किया गया था। मैंने अपने आवेदन से जेएआर को हटा दिया जिसने समस्या हल की। –

+2

आपने अभी मुझे बहुत समय और तंत्रिका बचाई है। बहुत अच्छा! – msteiger

+0

मुझे यह त्रुटि सार्वजनिक स्थैतिक विधि के लिए मिल रही है जो अब 2 तर्क लेता है। जब मैंने इसे बदल दिया तब पुरानी जार फ़ाइल मौजूद नहीं है। कोई विचार? – Markus

1

यह जब मैं एक जार एक और जार से एक कक्षा में एक निजी विधि का उपयोग करने की कोशिश में एक वर्ग के लिए किया था मुझे क्या हुआ: के बाद गूगल कैश से पूरा टेक्स्ट है। मैंने बस निजी विधि को सार्वजनिक, पुन: संकलित और तैनात करने के लिए बदल दिया, और यह बाद में ठीक काम किया।

0

एंड्रॉयड दृष्टिकोण से : विधि एपीआई संस्करण में उपलब्ध नहीं

मैं इस समस्या मुख्य रूप से हो रही थी क्योंकि मैं कुछ बात यह है कि उपलब्ध नहीं है/कि Android संस्करण

गलत में पदावनत उपयोग कर रहा था तरीका:

Notification.Builder nBuilder = new Notification.Builder(mContext); 
nBuilder.addAction(new Notification.Action(android.R.drawable.ic_menu_view,"PAUSE",pendingIntent)); 

सही तरीका:

Notification.Builder nBuilder = new Notification.Builder(mContext); 
nBuilder.addAction(android.R.drawable.ic_media_pause,"PAUSE",pendingIntent); 

यहां अधिसूचना।कार्रवाई नहीं उपलब्ध पूर्व करने के लिए एपीआई 20 है और मेरी मिनट संस्करण था एपीआई 16

0

बस हल जवाब देने के लिए एक अतिरिक्त:

यह एंड्रॉयड स्टूडियो के त्वरित रन की सुविधा के साथ एक समस्या हो सकती है, के लिए उदाहरण के लिए, अगर आपको एहसास हुआ कि आप कोड की लाइन जोड़ने के लिए भूल गए हैं: finish() किसी अन्य को खोलने के बाद आपकी गतिविधि में, और आपने उस गतिविधि को फिर से खोला है जिसे आपको फिर से खोलना नहीं चाहिए था (जिसे finish() हल किया गया था), तो आप finish() और तत्काल जोड़ते हैं रन होता है, तब तर्क टूट जाएगा क्योंकि ऐप क्रैश हो जाएगा।


टीएल: डीआर;

यह आवश्यक रूप से एक कोड बात नहीं, बस एक त्वरित चलाने समस्या

2

मैं एक स्प्रिंग बूट आवेदन जहां एक @RestController ApplicationInfoResource एक नेस्टेड वर्ग ApplicationInfo था पर इस त्रुटि हो रही थी नहीं है।

ऐसा लगता है कि Spring Boot Dev Tools एक अलग वर्ग लोडर का उपयोग कर रहा था।

अपवाद मैं हो रही थी

2017-05-01 17:47:39.588 WARN 1516 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.IllegalAccessError: tried to access class com.gt.web.rest.ApplicationInfo from class com.gt.web.rest.ApplicationInfoResource$$EnhancerBySpringCGLIB$$59ce500c

समाधान

मैं एक अलग जावा फाइल करने के लिए नेस्टेड वर्ग ApplicationInfo चले गए और समस्या से छुटकारा मिला।

संबंधित मुद्दे