I'm trying to convert a java object into an xml file.
this is the object's class:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
int eId;
String eName;
int eSalary;
public Employee(){
}
public Employee(int eId, String eName, int eSalary) {
this.eId = eId;
this.eName = eName;
this.eSalary = eSalary;
}
public int geteId() {
return eId;
}
public void seteId(int eId) {
this.eId = eId;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public int geteSalary() {
return eSalary;
}
public void seteSalary(int eSalary) {
this.eSalary = eSalary;
}
@Override
public String toString() {
return "Employee{"
"eId=" eId
", eName=" eName
", eSalary=" eSalary
'}';
}
}
While this is the class I made to convert the object:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
public class XMLConverter {
public static void main (String[] args){
try {
// create `Employee` object
Employee employee = new Employee();
employee.seteId(101);
employee.seteName("John Watson");
employee.seteSalary(30000);
// create an instance of `JAXBContext`
JAXBContext context = JAXBContext.newInstance(Employee.class);
// create an instance of `Marshaller`
Marshaller marshaller = context.createMarshaller();
// enable pretty-print XML output
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// create XML file
File file = new File("src//main//resources//employeebean.xml");
// convert employee object to XML file
marshaller.marshal(employee, file);
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
}
when i run the main method i geth this stack trace:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:81)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:115)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$GetterSetterReflection.optimize(Accessor.java:402)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:94)
at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:128)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:181)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:503)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:320)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:262)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:249)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:456)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:656)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
at XMLConverter.main(XMLConverter.java:18)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @6833ce2c
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:200)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:194)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.getMethod(Injector.java:184)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.access$000(Injector.java:69)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1.run(Injector.java:168)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1.run(Injector.java:165)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:164)
... 22 more
Process finished with exit code 1
Tried to search for a solution but still couldn't solve the problem.
Every suggestion is really uppreciated, thanks in advance
CodePudding user response:
You need to put this annotation on Employee class @XmlAccessorType(XmlAccessType.FIELD)
CodePudding user response:
Please add these dependencies to your pom.
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-ri</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>