java.lang.LinkageError when loading class from .zip file - java

I'm trying to make a class loader that will load .class files inside a .zip file. I followed this tutorial (https://weblogs.java.net/blog/2008/07/25/how-load-classes-jar-or-zip), but when i run the program, i get this error:
java.lang.LinkageError: loader (instance of org/freeforums/geforce/genforcer/main/ZipClassLoader): attempted duplicate class definition for name: "test/TestClass"
The error comes from line 30, which is:
return defineClass(filename, out.toByteArray, 0, out.size());
I call the method by using:
zipClassLoaderObj.findClass("test.TestClass");
Does anyone know what i'm doing wrong?

Found the problem. I was calling the method two times because it was in a 'for' loop with a bunch of other code. Just took it out of the loop, and now it works perfectly. Don't know why i couldn't figure that out myself. Thanks for the help!

Related

java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection

Since I updated to Java 11, I am getting an exception which is proving impossible to pin down.
Please note: The classes the exception mentions are nowhere to be found in my own code.
The stacktrace only shows which bit of my code meets the problem, but gives no context at all for the actual code (third party) that causes the problem.
This exception has been asked about before in Stackoverflow, but I can only solve the problem if I can find which third party code is causing this and upgrade it, which will (hopefully) mean that I can see the back of it.
The exception is as follows:
java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection (sun.net.www.protocol.https.HttpsURLConnectionImpl and com.sun.net.ssl.HttpsURLConnection are in module java.base of loader 'bootstrap')
I strongly suspected some very old apache HttpClient classes, and have replaced them with the java.net.http classes (available since Java 11), but to no avail.
The question is: is there a way to find which .jar contains code that uses HttpsURLConnectionImpl and HttpsURLConnection?
And does anybody know what the rather unhelpful indication that they are in module java.base of loader 'bootstrap' means?
are in module java.base of loader 'bootstrap'
It means that class sun.net.www.protocol.https.HttpsURLConnectionImpl is loaded by the bootstrap loader and are loaded as part of the module base. This is completely useless information: That module is the core java stuff (java.lang.String is in there too), and that loader is the one that loads that stuff. No wonder; sun.* classes are implementation details that are part of what all JDKs ship with out of the box. It provides nothing useful whatsoever for fixing the problem.
The exception is as follows:
There are only two options.
You forgot to paste the stack trace that follows the stuff you did paste. That stack trace is the important part, and lets you know exactly where to look.
You have broken exception handling.
I'm guessing it's option 2 - it's a common thing and perpetuated by manual tutorials. The fix is unfortunate: Go through all your code and fix the bad code. A search for .getMessage() should get you pretty far. It sure sounds like you are doing something like this:
try {
.. code here ..
} catch (Exception e) {
log.warn(e.getClass() + ": " + e.getMessage());
}
or similar. This is always bad - do not just 'log/print' and forget about an exception: You want any exception to be handled, OR thrown onwards. You never want to take an exception whose meaning is not clear as you write the code, and just 'swallow' it. Update your IDE's template settings for generating try/catch clauses; the proper way to write code that just sort of goes; "Exceptions? Um... I do not want to think about those right now / I dont know what they mean / I do know but there's nothing useful I can do about them at this point or I can't fathom how they could occur", which covers most of the time you need to deal with an exception, is like this:
try {
.. stuff ..
} catch (SomethingIDoNotWantToHandleException e) {
throw new RuntimeException("unhandled", e);
}
This will ensure that the place you see that error now has the actual stack trace which will then let you know which library you need to update.
The error was coming from pd4ml, a java library for producing pdfs.
I updated to the latest version (3.11.5), which solved the problem.

Android: Class in source code not found

I am trying to access the PhoneInterfaceManager.java on android.
As it is not part of the sdk, I need to use reflection. (I know I shouldn't do this, but as far as I know there is no other way).
The full name of the class is "com.android.phone.PhoneInterfaceManager".
Now when I run this code...
Object getPhoneInterfaceManager() throws ClassNotFoundException{
return Class.forName("com.android.phone.PhoneInterfaceManager");
}
...I get:
java.lang.ClassNotFoundException: com.android.phone.PhoneInterfaceManager
Note:
I checked the grep repository (which I find to be more reliable sometimes) and noticed that while the package for classes which worked with reflection were in the project "com.google.android / android", this class was to be found in "com.google.android / android-apps ". I don't know if this might mean something, but I thought I'd mention it anyway.
Thanks for your help in advance!

Java run time error.missing static main method

https://github.com/joywang1994/question2/blob/master/test2.java
This is my code. It has a run time error which says I don't have a static main method
Why? How could I fix it?
Thank you for your help!
I guess you are running wrong class file to start the program. I create 3 class file with the name HuffmanTree,HuffmanNode and Huffmancode. Copying it from your GITHUB. I ran Huffmancode class file. Program compile and runs with out error. I dont have C:\eclipse\Programmes\Datastructure\infix.dat this file so i got file not found exception. Ignoring that my main method called without any trouble.
If the message is:
Static Error: This class does not have a static void main method accepting String[].
then it follows that you are (somehow) running the wrong class. But unless you show us the complete stacktrace, and explain how you are running the code, we cannot tell you what you are doing wrong.

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

Java/Android - Crash - Apache commons-io-2.4.jar

I am trying to save values in a string to a file on my local system by using the org.apache.commons.io.FileUtils static method writeStringToFile.
So, first I downloaded the commons-io-2.4.jar, along with its javadocs and source and imported it into my Eclipse project through the Java Build Path. Everything compiles just fine.
However, when I add the simple line:
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File(Environment.getExternalStorageDirectory().toString() + "/logt.txt"), rslt.toString());
The program crashes. But, it doesn't crash anywhere near that statement. Instead, it crashes at the constructor of the Object which contains the method which calls this function.
In other words
Earlier in my code I create an object TranslateTask,
Which happens to contain a function call doTranslate().
The writeStringToFile call is made within the doTranslate() call,
However the actual crash takes place when I instantiate a TranslateTask object.
But, when I comment out the call to writeStringToFile(), the crash never happens
Even though the crash doesn't take place in the doTranslate() call...
So, just the mere mention of writeStringToFile() makes my program crash when I instantiate an object which contains it.
To make it more eary, I instantiate the object within a Try, catch (RejectedExecutionException e) block but instead the program crashes at this part:
PathClassLoader.findClass(String) line: 243 **Last call in the stack**
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573
PathClassLoader(ClassLoader).loadClass(String) line: 532
Translate$4.run() line: 159 **Crash happens here where I instantiate a TranslateTask object**
So PathClassLoader... Not sure how to approach debugging this. All I know is that if I commend out the org.apache.commons.io.FileUtils.writeStringToFile() call, the error never happens and the code runs fine everywhere.
Running this on the API8/10 of Android, using Eclipse Indigo.
EDIT- Logcat:
08-07 18:40:11.409: I/System.out(1395): debugger has settled (1363)
08-07 18:40:14.399: W/KeyCharacterMap(1395): No keyboard for id 0
08-07 18:40:14.399: W/KeyCharacterMap(1395): Using default keymap:
/system/usr/keychars/qwerty.kcm.bin
Don't think it gives any hint, but this is all that logcat gives after the debugger has settled.
EDIT2 -
For good measure, I just added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But still no go
EDIT3 -
I am starting to think the apache commons-io-2.4.jar doesn't work with Android inherently. Could that be it?
EDIT4 -
In case anyone wants to take a crack at it, here is my code. It is made in Windows 7, Eclipse Indigo. The code is based on the "Hello Android" translate section. All I am trying to do is extract some JSON into a string and save it into my sdcard. But, it has evolved into this mystery here...
https://dl.dropbox.com/u/10790286/Translate.zip (See TranslateTask.java line 111)
EDIT5 -
Interesting update. When I manually put FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); into the eclipse expressions box during a debug, I get the following error
An exception occurred: java.lang.NoSuchMethodError
I don't understand why I would get this error because 1) the WriteStringToFile() function is in the commons-io source. 2) I can instantiate a FileUtils object 3) I imported it into my program.
Whats going on here?
Writing to the path "c:\\temp\\logt.txt" isn't going to work on Android. You can use something like Environment.getExternalStorageDirectory().toString() + "/log.txt" to write to the SD card.
EDIT:
The follow worked on my Galaxy Nexus:
try {
FileUtils.writeStringToFile(new File(Environment.getExternalStorageDirectory(),"log2.txt"), "HelloNow");
} catch (IOException e) {
Log.e(TAG,e.getMessage())
}
Though the programme runs succesfully in complier ,the programme crashes while it runs in android device .Because during the run time it checks over jar files within android dependancies only. I too had similar type of problem .I got it solved when in preferences/properties -> java build .Tick the external jar file .

Categories

Resources