如果我尝试通过以下代码在 java 小程序中创建新的 xml 文档:
http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newInstance()
DocumentBuilderFactory.newInstance();
我会收到此错误:
Java Plug-in 1.6.0_19
Using JRE version 1.6.0_19-b04 Java HotSpot(TM) Client VM
javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> not found
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
我不关心 DTD。
随附的 html 文档如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Loading...</title>
</head>
问题出在实体解析器上,它指向 w3c.org 网站。访问参考 DTD 该网站已被限制用于应用程序的使用。这 解决方案是实现我自己的实体解析器。
相关:
如果您所做的只是调用
DocumentBuilderFactory.newInstance();
那么这不应该导致错误。您链接到的帖子不相关。
javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> not found
这表明存在一些奇怪的配置错误/错误。提供者应该(我认为)是 JAXP 实现的工厂类名称。检查您是否没有执行任何奇怪的操作,例如设置
javax.xml.parsers.DocumentBuilderFactory
系统属性或 Applet 类路径上有无效的 META-INF/services/javax.xml.parsers.DocumentBuilderFactory
文件。
JAVA FX SERVER
THREAD
public class ServerThread extends Thread {
// Server related fields.
private ObjectOutputStream oos;
private ObjectInputStream ois;
private ServerSocket server;
private Socket connection;
private int counter = 1;
private Displayer<String> displayer;
private Runnable onConnected;
private Runnable onDisconnected;
public ServerThread(Displayer<String> displayer, Runnable onConnected, Runnable onDisconnected) {
this.displayer = displayer;
this.onConnected = onConnected;
this.onDisconnected = onDisconnected;
}
protected void display(String message) {
if(displayer == null) return;
displayer.display(message);
}
@Override
public void run() {
try {
// Step 1: Create a ServerSocket.
server = new ServerSocket(5001, 100);
display("Started server: " + InetAddress.getLocalHost().getHostAddress());
while (true) {
// Step 2: Wait for a connection.
waitForConnection();
// Step 3: Get input and output streams.
getStreams();
// Step 4: Process connection.
readMessages();
// Step 5: Close connection.
closeConnection();
++counter;
}
} catch (
EOFException eofException) {
display("Client terminated connection");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
// Send message to client
public void sendMessage(String message) {
try {
message = "SERVER>>> " + message;
// Send String OBJECT to client.
oos.writeObject(message);
// Flush to ensure that data is pushed through stream to client.
oos.flush();
display(message);
} catch (IOException ioException) {
display("Error writing object. " + ioException.getMessage());
}
}
// Wait for connection to arrive, then display connection info
private void waitForConnection() throws IOException {
display("Waiting for connection...");
connection = server.accept();
display("Connection #" + counter + " received from: "
+ connection.getInetAddress().getHostName());
}
// Get streams to send and receive data
private void getStreams() throws IOException {
oos = new ObjectOutputStream(connection.getOutputStream());
oos.flush();
ois = new ObjectInputStream(connection.getInputStream());
display("Got I/O streams.");
}
// Process connection with client
private void readMessages() throws IOException {
// Send initial message to client.
String message = "Connection successful. Client #" + counter;
sendMessage(message);
// Connected
if(onConnected != null) onConnected.run();
do {
try {
message = (String) ois.readObject();
display(message);
} catch (ClassNotFoundException classNotFoundException) {
display("Unknown object type received.");
}
} while (!message.equals("CLIENT>>> TERMINATE"));
}
// close streams and socket
private void closeConnection() throws IOException {
display("User terminated connection.");
oos.close();
ois.close();
connection.close();
if(onDisconnected != null) onDisconnected.run();
}
}
MAIN JAVAFX
public class Server2JFX extends Application {
// Cached references to controls.
private TextField txtMessage;
private ObservableList<String> messages
= FXCollections.observableArrayList(new ArrayList<String>());
// Thread on which server runs.
private ServerThread serverThread;
private Scene createScene() {
// Main container.
BorderPane bp = new BorderPane();
// The area where the user will enter messages to be
// sent from the server to the client.
txtMessage = new TextField();
// Event handler used to send message to client.
txtMessage.setOnKeyPressed(event -> {
// If not the enter key, do nothing.
if(event.getCode() != KeyCode.ENTER) return;
// Send message to client.
String message = txtMessage.getText();
new Thread(() -> serverThread.sendMessage(message)).start();
// Clear text field.
txtMessage.setText("");
});
// Area to store messages received.
ListView<String> lbxMessages = new ListView<>();
lbxMessages.setItems(messages);
// Add controls to container.
bp.setTop(txtMessage);
bp.setCenter(lbxMessages);
// Return a new scene graph.
return new Scene(bp);
}
@Override
public void start(Stage primaryStage) throws Exception {
// Create scene.
Scene scene = createScene();
// Set stage details.
primaryStage.setTitle("Server 2 (JavaFX front-end)");
primaryStage.setWidth(300);
primaryStage.setMinHeight(500);
primaryStage.setScene(scene);
// Show stage.
primaryStage.show();
// Create the server thread.
serverThread = new ServerThread(
message -> Platform.runLater(() -> messages.add(message)), // How to display message.
() -> Platform.runLater(() -> txtMessage.setEditable(true)), // What to do when connected (run on UI thread).
() -> Platform.runLater(() -> txtMessage.setEditable(false)) // What to do when connected (run on UI thread).
);
// Start running the server.
serverThread.start();
}
public static void main(String[] args) {
launch(args);
}
}
package nmu.wrpv302.l08;
@FunctionalInterface
public interface Displayer<T> {
void display(T value);
}