Class not found exception when using Java class in c# - java

I convert a java's class to dll using IKVM, by making a jar file for the this class and then I used IKVM to make the conversion operation. this operation is done successfully without errors or warnings. then I add the (DLL) with an 'IKVM.GNU.Classpath.dll' and 'IKVM.Runtime.dll' (DLLs) to my project. then I rebuilt the project, up to that point no errors, no warnings, no crashes happened. but when I tried to deserialized an arraylist of object from it is type, I get an exception is : java.lang.ClassNotFoundException: "MyClass" not found in java.lang.ClassLoader... my deserliazation. code is :
object deserializedObject = null;
java.io.ObjectInputStream ina = new java.io.ObjectInputStream(new java.io.FileInputStream(#"D:\130043674485690000.txt"));
deserializedObject = ina.readObject();
ina.close();

First: you should use the current version of IKVM for new projects. The file name show that you use a very old version of IKVM.
Second: you need to use all dlls from the IKVM. If all is working then you can remove some of the not used dlls.
Third: if you does reference the classes of your dll/jar file statically then you need load it dynamically. See the wiki for details.

Related

Class cannot be resolved. It is directly referenced from required .class files

Hi I am trying to execute Update by query,i am using this code
UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
Script script = new Script("ctx._source.RELATIONSHIP_DEPTH = doc['"+AggregateValue_First+"'].value*doc['"+AggregateValue_Second+"'].value");
ubqrb.source(indexName)
.script(script)
.filter(QueryBuilders.matchAllQuery())
.get();
But I am getting these errors:
The type
org.elasticsearch.action.support.replication.IndicesReplicationOperationRequestBuilder
cannot be resolved. It is indirectly referenced from required
.class files
- The method source(String) is undefined for the type UpdateByQueryRequestBuilder
I have used the reindex.jar and I build my node like this:
node = nodeBuilder().clusterName(clusterName).client(true).node();
client = node.client();
I am using elasticsearch-2.3.4.jar and when I'm typing like this, I don't get any error:
UpdateByQueryClientWrapper ubqrb = new UpdateByQueryClientWrapper(client);
ubqrb.prepareUpdateByQuery().setScript("ctx._source.RELATIONSHIP_DEPTH = doc['"+AggregateValue_First+"'].value"+operator+"doc['"+AggregateValue_Second+"'].value");
The problem arises when I am using the query, which version of jar should I use for UpdateByQuery?
The type org.elasticsearch.action.support.replication.IndicesReplicationOperationRequestBuilder cannot be resolved. It is indirectly referenced from required .class files - The method source(String) is undefined for the type UpdateByQueryRequestBuilder
What this means is that a class in either your own code or in one of your direct dependencies is trying to use the class org.elasticsearch.action.support.replication.IndicesReplicationOperationRequestBuilder, which is currently not part of any .jar file that you have currently added as a dependency for your application.
With a bit of Googling (*) it can be found that this class used to be part of elasticsearch.jar, for instance in elasticsearch-1.5.1.jar.
Thus, you should go through your dependency setup and make sure that the versions of dependencies (and classes called on your own code) you're using are consistent with each other.
A build tool like Maven, Ivy, Gradle or Sbt for instance will make this a bit simpler. Via manual handling of dependencies it's more work, but still doable.
(*) How to find this out:
Search for the class in question
Open a link to JavaDocs. See the version of package this page is describing.

NoSuchMethodError after cleaning the project

I'm currently getting this error:
java.lang.NoSuchMethodError: org.json.JSONObject.keySet()Ljava/util/Set;
at ee.ut.cs.Parser.accessLint(Parser.java:39)
I have tried cleaning the project to no awail.
I suspect I have an error in the src/plugin/parse-htmlraw/build.xml while creating the jar file but I'm not certain. I understand that this error is because the function does not exist at runtime, but the object is created which means that the class is there, just not that function. I decompiled the .class file in created jar and it has the necessary functions.
Code is available at https://github.com/jaansusi/WCAGgrader
Q: What is wrong with the build that produces this error?
The problem is that even if I put the necessary class files in the jar I create, they are not linked correctly and the class that's called in the jar can't locate functions inside the other classes. The class object JSONObject is created but the functions inside the JSONObject class can't be found.
If you do not find the problematic version, there is a possibility you get it (especially if you are using Spring) from the following dependency -
<artifactId>android-json</artifactId>
<groupId>com.vaadin.external.google</groupId>
excluding it worked for me,
An easy way of analyzing dependencies is the maven-helper plugin in Intellij, see here
Check for the version you have used.
There might be a case where 2 different versions are being used which in turn causes this error.
To their own maven local repository com\Google\code\gson\gson, see if there are two or more version about json, will have to do is to delete the old, and remember to look at any other place in the project is introduced into the old version of the dependence, if any, change the old version of the dependence to the new version is perfectly solved this problem

JAVA creating custom .class library file from several existing ones ECLIPSE

I was using JD-GUI to get readable content of the several .class files in order to create a custom one, because many parts of the original libraries are not used (save space and performance)
So I read the code and started creating an eclipse library project in eclipse pasting them in.
soon apeared the first weird errors:
The method getLogger(Class) from the type Logger refers to the missing type Class
Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor
and so on.
Does anyone has experience in creating libraries from other libraries in eclipse? what is going wrong?
thank you.
EDIT:
it seems that several imports are not found:
error example:
The import org.w3c.dom.stylesheets cannot be resolved
how can this be, when the original libraries are working fine? I did not remove a single class. my custom library is just a merged one with 100% the same content.

Creating new classes from code

Is there a way to create a new java class during execution? All the information about the class (name, modifiers, methods, fields, etc.) exists. Now I want to create that class. An idea was to create a new file and write the stuff to that file, c'est fini! But I think there are more elegant ways to do that, maybe with JDT?
Either use BCEL to create byte code and class files (the hard way) or create the source code in memory and use the Java 6 Compiler API (that's what I would do). But with Compiler API you need a Java SDK while running the application, a JRE is not sufficient.
Further Reading
The Java 6.0 Compiler API
(There a lot of tutorials on the web)
If you are writing an eclipse plugin and you want your tooling to generate code into a project, you can interact with JDT using the AST. There is also a method to call the Eclipse batch compiler from your runtime.
AST ast = AST.newAST(AST.JLS3);
CompilationUnit unit = ast.newCompilationUnit();
PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
packageDeclaration.setName(ast.newSimpleName("example"));
unit.setPackage(packageDeclaration);
ImportDeclaration importDeclaration = ast.newImportDeclaration();
QualifiedName name =
ast.newQualifiedName(
ast.newSimpleName("java"),
ast.newSimpleName("util"));
importDeclaration.setName(name);
importDeclaration.setOnDemand(true);
unit.imports().add(importDeclaration);
TypeDeclaration type = ast.newTypeDeclaration();
type.setInterface(false);
type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
type.setName(ast.newSimpleName("HelloWorld"));
// ....
Long winded :-) but you have access to the JDT java core model as you go.
If you need to generate files into your eclipse workspace, there are also template based options, like JET.
But if you want to dynamically generate and load a .class file in the runtime of a java application try #Andreas_D suggestions.
Look at code generation libraries,
http://cglib.sourceforge.net/
http://www.csg.is.titech.ac.jp/~chiba/javassist/

in java using vtd-xml throws java.lang.UnsupportedClassVersionError

I have an ear app with two EJB and one WEB (war) projects all using compiler compliance 1.5. In my web project I created a utility Class file that takes a byte array of an xml file and returns some string values using the vtd-xml library v2.10 and I instantiate an object of this class in a servlet. At the call of the constructor of the object (that takes a byte array) I get the error:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
When I remove all vtd-xml related objects from the class I do not get this error. Any advice would be appreciated.
Regards
It sounds like the vtd-xml jar file has classes in it that are 1.6
you can recompile using the build.bat file (included with vtd-xml.zip) to obtain a new vtd-xml.jar.
The documentation says:
Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.
The library you are using must have been compiled with a different version of Java.
Now it works executing build.bat file (included with vtd-xml.zip) but you need to edit the target version to 1.5 instead of 1.6

Categories

Resources