java.lang.ClassFormatError while loading class - classloader

I am implementing the AbstractFactory pattern allowing outsiders to create Factories. I am reading the factory implementation name from a properties file, loading the factory class and using it to construct objects. Problem is that somewhere in my factory implementation when I'm trying to create an object of a task I get the following error:
Exception in thread "Thread-1" java.lang.ClassFormatError: Code
segment has wrong length in class file
com/testsuite/loadperformancetesterusage/tasks/GetUrl
Following is the GetUrl constructor, nothing fancy!
public GetUrl( String protocol, String port, String hostname, String path, String producerClassName ) {
super(producerClassName);
this.protocol = protocol;
this.hostname = hostname;
this.port = port;
this.path = path;
}
Any feedback on what might be happening here? Cross posted here

There was some mismatch in the version of classes i was using. It works now.

Related

QT C++ access to a Class Member of another Class

i really dont understand the following behavior:
3 Code snippets to explain
sqlconnection.h
class SqlConnection
{
public:
SqlConnection();
QSqlDatabase db;
} ;
sqlconnection.cpp
SqlConnection::SqlConnection()
{ if (!db.open()){ //no different with or without that
db = QSqlDatabase::addDatabase("QODBC");
...
}
artikelverwaltung.cpp
Artikelverwaltung::Artikelverwaltung(){
SqlConnection().db.open();
...(code to do works fine)
}
void Artikelverwaltung::on_pushButton_2_clicked()
{
SqlConnection().db.close(); // "HERE"
}
"HERE" it cycles the full Contructor of the Class again, but why it don't just close the connection? (this is the Main question about and how to fix that.)
I can't even access "db" from another Method inside the Class but outside of the Constructor, like:
sqlconnection.cpp:
void closeDB() {db.close();} is not possible.`
What i am doing wrong?
Thank you guys
In general, you do not need to use any wrapper class to hold database connections in Qt. You should create a database connection once using QSqlDatabase::addDatabase and open it. After that you can get a database connection anywhere by calling QSqlDatabase::database static method. So to close the database connection you should simply call QSqlDatabase::database().close().
Hello and welcome to Stackoverflow!
If you call SqlConnection().db.close();, what you are actually doing is creating a new instance of your SqlConnection class and then calling close() on its variable db. This is not the same one you opened earlier.
What you want to do is inside of your Artikelverwaltung class:
SqlConnection sqlConnection = new SqlConnection();
and then later
sqlConnection.db.open();
and afterwards
sqlConnection.db.close();
The reason why you cant add that closeDB() method is that you need to either put the method inside of the SqlConnection class or you need to operate on an instance of the class.
You cannot operate on a class like this without instanciating it (creating an instance of it).
You should look at class instantiation to understand how the concept works.
If you call SqlConnection() you are creating a new instance of the class (you can look at it like a copy of the class).
If you call SqlConnection() somewhere else you are simply creating a new instance.
These instances do NOT share the variables or the contents in them. They are completely independent.
You cannot access a member of a class (like your db variable) without creating an instance of the class first and then accessing the member of that instance.

Determine correct protobuf class

In my existing ZeroMQ application am playing to change my payload to google protobuf. During Initial analysis problem I am facing is i will have a multiple proto classes,which can be written to log file also. Now in runtime how do I determine which proto is received. And also later at some point if I read the log file here also I will face the same problem. Is there any solution to this? In both proto2 and proto3
Protocol Buffer does support multiple class definition. The way you do it is by declaring all the types of classes you have in your Protocol Format file.
In this example we declare two different classes and their properties:
syntax = "proto2";
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
message House {
repeated string owner = 1;
}
If you are wailing to use protocol buffer as your standard communication protocols I guess you want to send one of the classes. See the official docs where they explain how to deal with this situation in this post.
Another good pattern is to create a unique message (Wrapper) that holds common properties and one of the classes:
message WrapperMessage {
required int64 timestmap = 1;
oneof data {
Person person = 2;
House house = 3;
}
}
Then before decoding the class you can check which type of class the wrapper holds by calling HasField.
Note: Protocol Buffers 3 implements a new feature for this purpose based on the FileDescriptorSet and Any property.

ArrayOfXXX class out of soap input param of array type

I have a method with input param as Array. When I generate stub out of it creates List type.
But I want to know how to create a wrapper class around array type e.g. for class Apple it should create ArrayOfApple.
Is there any change needs to be done in class or any specific plugin need to be used?
Note: I am using JAXWS with Apache CXF implementation
Below is the sample code:
EmployeeService.java:
#WebService(endpointInterface="com.test.EmployeeService")
#SOAPBinding(style=Style.DOCUMENT)
public class EmployeeService {
public String updateEmpRoles(#WebParam(name="EmpRoles")EmpRole[] empRoles) {
return "SUCCESS";
}
}
EmpRole.java :
#XmlType(name="EmpRole")
public class EmpRole {
private String empRole;
public String getEmpRole() {
return empRole;
}
public void setEmpRole(String empRole) {
this.empRole = empRole;
}
}
After publishing, wsdl is getting generated as below -
But what I expect is WSDL should create ArrayOfEmpRole and it should wrap List<EmpRole>.
Kindly help
In short - I want something that Björn doesn't want in below link. (In his case, it's automatically creating ArrayOfXXX, this is what I need) - Arrays in SOAP method Parameters generated via JAX-WS?
I would switch from Code first to a Contract first approach which means start with the WSDL and generate a stub using wsdl2java from it. This way you can ensure that the WSDL looks like the way you want.
If you want to stick to the current approach, the easiest way to achieve a wrapper is probably to introduce another class.

Spring Data Neo4j #RelatedToVia error on save()

I'm getting the following error when I try to save an entity which has a #RelatedToVia attribute:
org.springframework.dao.InvalidDataAccessApiUsageException: Null parameter, startNode=NodeImpl#1, endNode=null, type=DynamicRelationshipType[BPA_PROPOSITION]; nested exception is java.lang.IllegalArgumentException: Null parameter, startNode=NodeImpl#1, endNode=null, type=DynamicRelationshipType[BPA_PROPOSITION]
From the error description above it seems that my RelationshipEntity is missing the end node. However, and this is the worst part of the problem, this is not true because I get this error randomly.
Here is the scenario. I'm creating some really simple tests just to check my class mappings. I manually create the classes necessary for each test case and then I save them. As Spring Data "cascades" the persistence of the entities my only concern is to populate the entity under test with its primitive properties and related entities, save it and then retrieve it back to see if the data is there.
This worked well for my first few classes which do not have #RelatedToVia mappings, but not for the ones which use #RelatedToVia. Here are some excerpts from the code which uses #RelatedToVia.
#NodeEntity
public class BasicProbabilityAssignmentFunction {
#GraphId
private Long id;
#RelatedTo(type = RelTypeConstants.BPA_FRAME, direction = Direction.OUTGOING)
private FrameOfDiscernment frameOfDiscernment;
#RelatedToVia(type = RelTypeConstants.BPA_PROPOSITION, direction = Direction.OUTGOING, elementClass = Belief.class)
private Set<Belief> beliefs;
}
#RelationshipEntity
public class Belief {
#GraphId
private Long id;
#StartNode
private BasicProbabilityAssignmentFunction bpaFunction;
#EndNode
private Proposition proposition;
}
#NodeEntity
public class Proposition {
#GraphId
private Long id;
#RelatedTo(type= RelTypeConstants.PROPOSITION_HYPOTHESIS, direction= Direction.OUTGOING)
private Set<Hypothesis> hypotheses;
#RelatedTo(type = RelTypeConstants.FRAME_PROPOSITION, direction = Direction.INCOMING)
private FrameOfDiscernment frameOfDiscernment;
}
Plus, here is an image of the variables state in debbuging mode just before calling the BasicProbabilityAssignmentFunction repository save. Notice that the Belief entity is fully populated!
And also the code used for test:
//this just creates an instance with its attributes populated
BasicProbabilityAssignmentFunction bpaFunction = BasicMockFactory.createBpaFunction();
//this is where I get the error.
bpaFunction = bpaFunctionRepository.save(bpaFunction);
One further note! I managed to stop getting this error by saving all entities (e.g., Proposition, Hypothesis etc) related to BasicProbabilityAssignmentFunction before saving BasicProbabilityAssignmentFunction itself. Nevertheless, I'm not sure why this solved the problem.
Answering Michael comment: Michael, are you saying that the rel-type should be defined in the Belief class itself (instead of using the type property of the #RelatedToVia annotation) or otherwise I should use template.createRelationshipBetween? I tried to use the #RelationshipEntity type property, but the problem persisted. What worked was saving the relationship #EndNode (Proposition) before the #Startnode (BasicProbabilityAssignmentFunction). By doing this, the Belief relationship is created (saved) without problem when the BasicProbabilityAssignmentFunction is saved.
This problem is already known. Please have a look at the following links:
http://forum.springsource.org/showthread.php?124316-Spring-Data-Neo4j-NullPointerException-with-RelatedToVia
https://jira.springsource.org/browse/DATAGRAPH-216

Java type casting

I have three files:
ScriptProcessor.java:
public interface ScriptProcessor {
public String someMethod();
}
ScriptProcessorDummy.java:
public class ScriptProcessorDummy implements ScriptProcessor {
public String someMethod{
return "some string";
}
}
In the main method, the code does the following:
URLClassLoader loader = null;
loader = new URLClassLoader(new URL[] {new URL("JARFILE")});
if (loader != null) {
ScriptProcessor processor = (ScriptProcessor) loader.loadClass("ScriptProcessorDummy").newInstance();
}
"JARFILE" contains class files of ScriptProcessor and ScriptProcessorDummy.
The code works fine when using JDK 1.4 but when using JDK 1.5, the typecast (to ScriptProcessor) fails with java.lang.ClassCastException.
Could somebody please tell me how to fix this.
Thanks,
Raj
You need to make your current class loader the parent of the new class loader you are loading, and make sure that there isn't a copy of the interface in your jar.
The problem you have suggests that you have two copies of the interface: one in the main class loader, one in your new one. The object returned uses the one in the separate jar, but your class is using the main one. They aren't the same. You have to make sure that Java uses the same '.class' for the interface when processing the loaded class as it did when it compiled your code.
The first thing to do is 'jar tf' on the jar and see if my hypothesis of two copies is correct. If so, remove it. Try running. If you now get a NoClassDef, fix the construction of the loader.
new URLClassLoader(urlArray, Thread.currentThread().getContextClassLoader());
assuming that your environment maintains the context class loader. Alternatively,
new URLClassLoader(urlArray, ScriptProcessor.class.getClassLoader());
Try using:
loader.loadClass("ScriptProcessorDummy", true).newInstance();
the boolean tell the classloader to resolve the class name.