I am trying to establish a XMPP connection using the Smack library. Using the version 4.2.0-beta2(smack-core) and 4.1.8(smack-tcp)
public void gcmConnect()
{
try{
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder().setHost(GCM_SERVER)
.setPort(GCM_PORT).setUsernameAndPassword("27614215340#gcm.googleapis.com", API_KEY).build();
XMPPTCPConnection connection = new XMPPTCPConnection(config);
connection.connect();
System.out.println("Connected");
}
catch(XMPPException ex)
{
ex.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XMPPConnectionTest obj = new XMPPConnectionTest();
obj.gcmConnect();
}
It gives the following error
Exception in thread "main" java.lang.NoSuchFieldError: ifpossible
at org.jivesoftware.smack.ConnectionConfiguration$Builder.<init>(ConnectionConfiguration.java:438)
at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration$Builder.<init>(XMPPTCPConnectionConfiguration.java:91)
at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration$Builder.<init>(XMPPTCPConnectionConfiguration.java:87)
at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:80)
at com.inn.foresight.gcm.XMPPConnectionTest.gcmConnect(XMPPConnectionTest.java:32)
at com.inn.foresight.gcm.XMPPConnectionTest.main(XMPPConnectionTest.java:53)
These are the possible reasons of getting this Exception
1. You probably compile using one version of a library, but use another version at run-time . You must verify that your classpath contains the proper version of the specified library.
2. You might have got two versions of jar being used.
3. Incomplete jar file
Related
I want to implement config cat in my java 8 project.
The way I am creating a config cat client is this:
import com.configcat.ConfigCatClient;
public class ConfigCatClientUtil {
public ConfigCatClientUtil() {
}
public ConfigCatClient createClient(String configCatKey) {
ConfigCatClient configCatClient = null;
try {
configCatClient = new ConfigCatClient(configCatKey);
} catch (Exception e) {
}
return configCatClient;
}
}
The problem is that when try to execute this new ConfigCatClient(configCatKey) throws an error:
messages Feb 11 13:11:38 server: Caused by: java.lang.NoClassDefFoundError: okhttp3/Callback
I am using configcat-java-client-6.0.1.jar.
Does anyone know why I am getting this error?
This library has a direct dependency on OkHttp
https://mvnrepository.com/artifact/com.configcat/configcat-java-client/6.0.1
If you are following their documentation you wouldn't see this, so I suggest starting with their instructions for setting up your maven or Gradle build.
https://configcat.com/docs/sdk-reference/java/
I have recently upgraded opensaml dependency from 2.5.3 to 2.6.1 and xmlutil from
1.3.0 to 1.4.1.
It compiles without any errors but while running the application i get the following exception:
java.lang.NullPointerException
org.opensaml.xml.io.AbstractXMLObjectMarshaller.marshall(AbstractXMLObjectMarshaller.java:84)
Calling Code:
final MarshallerFactory marshallerFac = SAMLUtil.getMarshallerFactory();
final org.opensaml.xml.io.Marshaller authnStatementMarshaller = marshallerFac.getMarshaller(assertion);
Element assertionElement = null;
try {
assertionElement = authnStatementMarshaller.marshall(assertion);
try {
// Sign assertion and query signature
Signer.signObject(signature);
}
catch (final SignatureException e) {
LOGGER.error("Fout opgetreden bij ondertekenen Assertion", e);
}
}
I've noticed that if you don't initialize ("bootstrap") the SAML configuration, you get a NullPointerException (rather unhelpfully, I might add) when you try to construct the SAML.
import org.opensaml.DefaultBootstrap;
import org.opensaml.xml.ConfigurationException;
try {
DefaultBootstrap.bootstrap();
}
catch (ConfigurationException ce) {
}
The above is just a snippet of code to illustrate what I'm talking about. Did you maybe forget to bootstrap the configuration? That has to be done before you do anything.
While the build paths are not correct I obtain “com.microsoft.sqlserver.jdbc.SQLServerDriver” from the stack trace. As they are built correctly, I obtain my printed statement “Successfully connected”. The JDBC is living within the getter/setters of the webservice as a method.
When I place the JDBC content in its own file with no builds and run as a java application I receive: “com.microsoft.sqlserver.jdbc.SQLServerDriver”
When I place the JDBC content in its own file with builds and run as a java application I receive: “Successfully connected”
When the method is called from a test file as a java application I receive: “Successfully connected”
Ex:
public static void main(String[] args) {
insert.main(args);
When the method is run as a java application on PO I receive: “Successfully connected”
When I place the method to be called under a setter (which will be invoked by the client, which will cause the jdbc to be invoked) I receive: “com.microsoft.sqlserver.jdbc.SQLServerDriver”
Would you happen to have any tips for me? I’m clueless why it will work under being invoked as an application but not via client?
public class insert{
public static void main(String[] args) {
Connection con = null;
Statement st = null;
final String DB_URL = "jdbc:jtds:sqlserver://00.00.00.00:0000/DB";
// Database credentials
final String USER = "usrname";
final String PASS = "pw";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(DB_URL, USER, PASS);
st = con.createStatement();
System.out.println("successfully connected!");
} catch (Exception err) {
System.out.println(" " + err.getMessage ());
}
finally {
try {
con.close();
} catch (Exception e) { /* ignored */ }
try {
st.close();
} catch (Exception e) {
/* ignored */
}
}
}
}
Any tips at this point would be greatly appreciated.
The problem is that your jar misses the necessary libraries that provides com.microsoft.sqlserver.jdbc.SQLServerDriver class and others to communicate with your SQL server. You have to make sure the library is loaded and available when is being executed from tomcat. Just copy your library and drop it inside %TOMCAT_INSTALL%/lib folder, where %TOMCAT_INSTALL% is the folder where your tomcat is installed, so the library will be available for every project (war, jar, etc) that runs in your tomcat installation.
I'm following this tutorial to establish a WebSocket connection to a server:
http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html
The code (same as the tutorial):
import java.net.URI;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
/**
* Example of a simple Echo Client.
*/
public class SimpleEchoClient {
public static void main(String[] args) {
String destUri = "ws://echo.websocket.org";
if (args.length > 0) {
destUri = args[0];
}
WebSocketClient client = new WebSocketClient();
SimpleEchoClient socket = new SimpleEchoClient();
try {
client.start();
URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
System.out.printf("Connecting to : %s%n", echoUri);
// socket.awaitClose(5, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
client.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Errors:
2014-08-07 21:49:00.346:INFO::main: Logging initialized #86ms
org.eclipse.jetty.websocket.api.InvalidWebSocketException:
SimpleEchoClient is not a valid WebSocket object.
Object must obey one of the following rules:
(1) class implements org.eclipse.jetty.websocket.api.WebSocketListener or
(2) class is annotated with #org.eclipse.jetty.websocket.api.annotations.WebSocket
at org.eclipse.jetty.websocket.common.events.EventDriverFactory.wrap(EventDriverFactory.java:145)
at org.eclipse.jetty.websocket.client.WebSocketClient.connect(WebSocketClient.java:200)
at org.eclipse.jetty.websocket.client.WebSocketClient.connect(WebSocketClient.java:144)
at SimpleEchoClient.main(SimpleEchoClient.java:31)
I'm not too sure what is wrong with my imported jar file. Maybe it is the wrong one? I'm using this: http://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-client/9.2.2.v20140723
Surely there must be an easier way to establish a connection via Jetty Websocket and start receiving data?
As Kayman explained in the comment, your problem with the socket handler implementation, use the latest release here explained with an example(same you used but correct) http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html
It looks like the documentation is out-of-date with the current version you are using. Try rolling back to a more stable version of 9.2.x like:
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.2.0.RC0</version>
</dependency>
I am getting an exception while using SSHJ.
Here is how I implemented it:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("serverName");
try{
ssh.authPublickey("myUserId");
final Session session = ssh.startSession();
try{
final Command cmd = session.exec("net send myMachineName Hello!!!");
System.out.println(cmd.getOutputAsString());
System.out.println("\n Exit Status: "+cmd.getExitStatus());
}finally{
session.close();
}
}finally{
ssh.disconnect();
}
}
}
But I get the following exception:
Exception in thread "main" java.io.IOException: Could not load known_hosts
at net.schmizz.sshj.SSHClient.loadKnownHosts(SSHClient.java:528)
at SSHTEST.main(SSHTEST.java:25)
What am I doing wrong?
Use the folowing code
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(
new HostKeyVerifier() {
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true; // don't bother verifying
}
}
);
ssh.connect("LocalHost");
Remove the call to loadKnownHosts() method, which as erickson mentioned checks under ~/.ssh/known_hosts by default (you can specify the location as an argument as well though), and replace it with:
ssh.addHostKeyVerifier("public-key-fingerprint");
To find out what the fingerprint is, the twisted way would be to connect without that statement - you'll find out from the exception ;-)
It sounds like it's trying to read a "known_hosts" file, but can't find it, or possibly it in an invalid format.
The SSH known hosts file records the public key for various hosts to thwart some spoofing attacks. Normally it resides in ~/.ssh/known_hosts. Try creating an empty file there and see if that satisfies the library.
The library documentation is likely to address the necessary configuration files.