英文:
NetBeans java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory] at javax.xml.bind.JAXB.unmarshal(JAXB.java:171)
问题
我已将NetBeans更新到12版本。我的应用程序是在Java 8中创建的。我现在使用Java 14。一切都在运行,但出现了这个错误:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.JAXB.unmarshal(JAXB.java:171)
我在Google上阅读了许多帖子,但我无法找到解决方案。
只有在我运行NetBeans应用程序时才会出现此错误。如果我编译我的应用程序并运行它,它可以正常运行,没有错误。
我还下载了jaxb-api-2.3.1.jar并添加到库中。
编辑:还尝试了jaxb-api-2.4.0-b180830.0359.jar。没有成功。
但真正疯狂的是,如果我在终端中运行我的应用程序,我会得到这个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXB
只有在我当前文件夹中点击myApp.jar时,它才能正常运行,没有任何错误:
有人有什么想法吗?
对于这种行为,有什么解决办法吗?
提前致谢。
编辑:
目前我找到了一个解决办法,但是伴随着这个警告:
警告:发生了非法的反射访问操作
警告:非法的反射访问来自com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1 (file:/Applications/NetBeans/Apache%20NetBeans%2012.0.app/Contents/Resources/NetBeans/netbeans/ide/modules/ext/jaxb/jaxb-impl.jar)对方法java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)的访问,java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
警告:请考虑将此情况报告给com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1的维护者
警告:使用--illegal-access=warn来启用进一步的非法反射访问操作警告
警告:在将来的版本中,所有非法访问操作都将被拒绝
如果我使用此JAXB 2.2.5.jar库,就会出现这个警告。
不适用上述描述的JAXB版本。
我的应用程序现在使用2.2.5运行,但还能坚持多久?
抱歉,但我不知道如何通知开发人员。我真的认为他们知道这个问题。但遗憾的是,它还没有被修复。 :(
这是我的代码:
```java
package Tools;
import Enums.Property;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import static Tools.Device.Director;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Device {
@Override
public String toString() {
return DeviceName;
}
public static final String Separator = FileHandler.getFileSeparator();
public static File Director = new File(Property.datarefBaseFile.getValue().substring(0, Property.datarefBaseFile.getValue().lastIndexOf(Separator)) + Separator + "Devices" + Separator);
@XmlElement
public String DeviceName;
@XmlElement
public String DevicePosX;
@XmlElement
public String DevicePosY;
@XmlElement
public String DeviceWidth;
@XmlElement
public String DeviceHeight;
@XmlElement
public String DeviceBackgroundImage;
@XmlElement
public String DeviceScaleX;
@XmlElement
public String DeviceScaleY;
public List<ControlProps> controlsProps;
public static Device load(String file) {
return JAXB.unmarshal(new File(Director, file), Device.class);
}
public static File getDirector() {
return Director;
}
public void save() {
Director.mkdir();
File file = new File(Director, DeviceName + ".xml");
File fileback = new File(Director, DeviceName + ".bak");
if (fileback.exists()) {
fileback.delete();
System.out.println(String.format("Device %s old deleted! %s", fileback, Utils.ShowTime()));
}
file.renameTo(fileback);
if (fileback.exists()) {
System.out.println(String.format("Device %s new created! %s", fileback, Utils.ShowTime()));
}
if (!file.exists()) {
JAXB.marshal(this, file);
String msg = String.format("Device %s saved! %s", file, Utils.ShowTime());
System.out.println(msg);
}
if (!file.exists()) {
String msg = String.format("Fatal error: %s was deleted an not created new!\nDevice crashed.\nRestore from %s file. %s", file, fileback, Utils.ShowTime());
System.out.println(msg);
MsgBox.error(msg);
}
if (!fileback.exists()) {
String msg = String.format("Warning! Backupfile: %s was not created yet! %s", fileback, Utils.ShowTime());
System.out.println(msg);
MsgBox.error(msg);
}
// JOptionPane.showMessageDialog(null, msg);
// LogFile.log(Level.INFO, new String[] {file.toString() + " saved"});
}
public int delete() {
File fileBak = new File(Director, DeviceName + ".creater.bak");
File file = new File(Director, DeviceName + ".xml");
int response = 0;
String msg = String.format("Delete this device: %s?", DeviceName);
response = JOptionPane.showConfirmDialog(null, msg, "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION && file.exists()) {
file.renameTo(fileBak);
file.delete();
msg = String.format("File: %s deleted\nBackupfile %s was created!", file, fileBak);
JOptionPane.showMessageDialog(null, msg);
} else {
msg = String.format("%s not deleted file: %s", DeviceName, file);
JOptionPane.showMessageDialog(null, msg);
}
return response;
}
public Device() {
controlsProps = new LinkedList<>();
}
public ControlProps find(String name) {
for (ControlProps prop : controlsProps) {
if (prop.ControlName.equals(name)) {
return prop;
}
}
throw new IllegalArgumentException("ControlName not found");
}
public void deleteControl(ControlProps controlProps
<details>
<summary>英文:</summary>
I have updated NetBeans to version 12. My Application was created in Java 8. I use now java 14. All is running but this:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.JAXB.unmarshal(JAXB.java:171)
I raead many posts in Google but i can’t hit a solution.
This error occurs only if i run in NetBeans app. If i compile my app and run it it works without errors.
I habe also downloaded jaxb-api-2.3.1.jar and added to library.
Edit: Alos tryed jaxb-api-2.4.0-b180830.0359.jar. No success.
But realy crasy is, if i run my app in Terminal i get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXB
Only on click on myApp.jar in my current folder it runs without any error:
Has any body an idea?
What is a solution for this behavior?
Thanks in advance.
Edit:
For now i found a solution but whith this WARNING:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1 (file:/Applications/NetBeans/Apache%20NetBeans%2012.0.app/Contents/Resources/NetBeans/netbeans/ide/modules/ext/jaxb/jaxb-impl.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
this appears if i use this JAXB 2.2.5.jar library.
Don’t work wit JAXB versions as described above.
My app runs now with 2.2.5 but how long?
Sorry but i have no idea how i can inform the developer. i.g. Sun or Marven or whoever. I true this issue is known there. But i’s bad that it isn’t fixt yet. :(
This ist my code:
package Tools;
import Enums.Property;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import static Tools.Device.Director;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Device {
@Override
public String toString() {
return DeviceName;
}
/**
*
*/
public static final String Separator = FileHandler.getFileSeparator();
public static File Director = new File(Property.datarefBaseFile.getValue().substring(0, Property.datarefBaseFile.getValue().lastIndexOf(Separator)) + Separator + "Devices" + Separator);
@XmlElement
public String DeviceName;
@XmlElement
public String DevicePosX;
@XmlElement
public String DevicePosY;
@XmlElement
public String DeviceWidth;
@XmlElement
public String DeviceHeight;
@XmlElement
public String DeviceBackgroundImage;
@XmlElement
public String DeviceScaleX;
@XmlElement
public String DeviceScaleY;
public List<ControlProps> controlsProps;
public static Device load(String file) {
return JAXB.unmarshal(new File(Director, file), Device.class);
}
public static File getDirector() {
return Director;
}
public void save() {
Director.mkdir();
File file = new File(Director, DeviceName + ".xml");
File fileback = new File(Director, DeviceName + ".bak");
if (fileback.exists()) {
fileback.delete();
System.out.println(String.format("Device %s old deleted! %s", fileback, Utils.ShowTime()));
}
file.renameTo(fileback);
if (fileback.exists()) {
System.out.println(String.format("Device %s new created! %s", fileback, Utils.ShowTime()));
}
if (!file.exists()) {
JAXB.marshal(this, file);
String msg = String.format("Device %s saved! %s", file, Utils.ShowTime());
System.out.println(msg);
}
if (!file.exists()) {
String msg = String.format("Fatal error: %s was deleted an not created new!\nDevice crashed.\nRestore from %s file. %s", file, fileback, Utils.ShowTime());
System.out.println(msg);
MsgBox.error(msg);
}
if (!fileback.exists()) {
String msg = String.format("Warning! Backupfile: %s was not created yet! %s", fileback, Utils.ShowTime());
System.out.println(msg);
MsgBox.error(msg);
}
// JOptionPane.showMessageDialog(null, msg);
// LogFile.log(Level.INFO, new String[] {file.toString() + " saved"});
}
public int delete() {
File fileBak = new File(Director, DeviceName + ".creater.bak");
File file = new File(Director, DeviceName + ".xml");
int response = 0;
String msg = String.format("Delete this device: %s?", DeviceName);
response = JOptionPane.showConfirmDialog(null, msg, "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION && file.exists()) {
file.renameTo(fileBak);
file.delete();
msg = String.format("File: %s deleted\nBackupfile %s was created!", file, fileBak);
JOptionPane.showMessageDialog(null, msg);
} else {
msg = String.format("%s not deleted file: %s", DeviceName, file);
JOptionPane.showMessageDialog(null, msg);
}
return response;
}
public Device() {
controlsProps = new LinkedList<>();
}
public ControlProps find(String name) {
for (ControlProps prop : controlsProps) {
if (prop.ControlName.equals(name)) {
return prop;
}
}
throw new IllegalArgumentException("ControlName not found");
}
public void deleteControl(ControlProps controlProps) {
String name = controlProps.ControlName;
if (controlsProps.remove(controlProps)) {
save();
System.out.println(name + " deleted");
//LogFile.log(Level.INFO, new String[]{name + " deleted"});
}
}
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论