Error message: try-with-resources is not supported in -source 1.5
Full error message:
\App\workspace_eclipse\EmpressFinance\src\main\java\com\empress\jpmc\chat\chatroom.java:[58,5] error: try-with-resources is not supported in -source 1.5
code:
private String buildJsonData(String username, String message) {
JsonObject jsonobject = Json.createObjectBuilder().add("message",username+ ": "+message).build();
StringWriter stringWritter = new StringWriter();
try(JsonWriter jsonwritter = Json.createWriter(stringWritter))
{
jsonwritter.write(jsonobject);
}
return stringWritter.toString();
}
full code of java:
package com.empress.jpmc.chat;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/chatroomServerEndpoint")
public class chatroom {
static Set<Session> chatroomUser = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void handleOpen(Session userSession)
{
chatroomUser.add(userSession);
}
@OnClose
public void handleClose(Session userSession)
{
chatroomUser.remove(userSession);
}
@OnMessage
public void handleMessage(String message, Session userSession) throws IOException
{
String username = (String) userSession.getUserProperties().get("username");
if (username==null)
{
userSession.getUserProperties().put("username", message);
userSession.getBasicRemote().sendText(buildJsonData("System","You are connected as" + message ));
}
else
{
Iterator<Session> iterator = chatroomUser.iterator();
while (iterator.hasNext()) iterator.next().getBasicRemote().sendText(buildJsonData(username, message));
}
}
private String buildJsonData(String username, String message) {
JsonObject jsonobject = Json.createObjectBuilder().add("message",username+ ": "+message).build();
StringWriter stringWritter = new StringWriter();
try(JsonWriter jsonwritter = Json.createWriter(stringWritter))
{
jsonwritter.write(jsonobject);
}
return stringWritter.toString();
}
}
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
Adding this snippet to your pom.xml will fix the issue.
Yes, vinoth10 right! pom.xml must include, something like:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.