Error: javax.wireless.messaging does not exist - java

I'm not java developer, but I need to test a code. I installed jdk and i tested Hello Word it worked fine but When i try to javac this another script, I'm gettings this error.
My Code:
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import javax.microedition.io.Connector;
import java.io.IOException;
public class SMSUtility {
public static void sendMessage(String msisdn, String text)
throws IOException {
// Open connection
MessageConnection con = (MessageConnection)
Connector.open("sms://+" + msisdn);
// Create new message
TextMessage message = (TextMessage)
con.newMessage(MessageConnection.TEXT_MESSAGE);
// Set text
message.setPayloadText(text);
// Send message
con.send(message);
// Close connection
con.close();
}
}

javax.wireless is not part of the SDK. Read about the Wireless Messaging API.

javax.messaging is in wma20.jar, but i can't figure out, how to add it to buildpath with ant...

Related

Why I can't call methods outside of my websocket server in Java?

I have websocket server class where I want to print when user connects. It works without a problem, however if I want to execute a method outside of this websocket server, it doesn't print and closes the client's connection and prints out java RuntimeException error.
package com.rupla.myapp.endpoint;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.rupla.myapp.encoders.MessageEncoder;
import com.rupla.myapp.http.MyOtherClas;
import com.rupla.myapp.encoders.MessageDecoder;
#ServerEndpoint(value = "/chatnow", encoders = MessageEncoder.class, decoders = MessageDecoder.class)
public class WebSocketServer {
#OnOpen
public void onOpen(Session session) {
extracted(session);
}
private void extracted(Session session) {
System.out.println("Session Opened (Client to Server) - ID: " + session.getId());
}
#OnMessage
public void handleTextMessage(String message, Session s) throws IOException {
System.out.println("New Text Message Received: " + message + " - From session: " + s.getId());
//So here we send client's message to the client - This works
s.getBasicRemote().sendText("Server: " + message);
try {
// Here I try to print "hello" but from 'MyOtherClas'.class using printTest() method
MyOtherClas.printTest();
//Right after this code, server closes the connection to the client and doesn't print anything
}
catch(Exception e) {
System.out.println("fail: "+e);
}
}
/**
* If I would call this method below, it would work since it's in the same class (but why?)
*/
private void printInsideClas() {
System.out.println("hello");
}
#OnClose
public void onClose(Session session) throws IOException, EncodeException {
System.out.println("Session Closed");
}
#OnError
public void onError(Throwable t) {
System.out.println("onError: " + t);
}
}
Pretty basic websocket server for java. However when I run this on my tomcat 9.0 and connect with javascript client and try to send message from my client to the server. It closes the connection between server and client and then prints this error.
Session Opened (Client to Server) - ID: 4
New Text Message Received: asd - From session: 4
onError: java.lang.RuntimeException: org/apache/http/Header
Session Closed
I tried to google this for few days for now but still I have no answers, I really need help with this.
The exception is in a server-side webservices stack. The exception is saying that it cannot load a core class that is part of Apache HTTPComponents.
Here are some possible explanations:
Your server-side runtime is missing the JAR file.
The JAR is present, but it is not on the right runtime classpath.
The Header class depends on some other class that has failed class initialization. If this has happened, there should be evidence earlier in the logfile.
The JAR file you should be looking for is "httpcore-.jar" or the equivalent from the old Jakarta project.
Figure out which of the above has happened, and you are close to solving the problem.

Java Current Directory Returns `null`

I'm trying to use the printWorkingDirectory() from Apache Commons FTP but it's only returning null. I can't navigate directories, list files, etc.
Log in pass all is success but how ever I try I can not change current directory.
I use this following code:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownloadFileDemo {
public static void main(String[] args) {
String server = "FTP server Address";
int port = portNo;
String user = "User Name";
String pass = "Pasword";
FTPClient ftpClient = new FTPClient();
String dir = "stocks/";
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
System.out.println( ftpClient.printWorkingDirectory());//Always null
//change current directory
ftpClient.changeWorkingDirectory(dir);
boolean success = ftpClient.changeWorkingDirectory(dir);
// showServerReply(ftpClient);
if (success)// never success
System.out.println("Successfully changed working directory.");
System.out.println(ftpClient.printWorkingDirectory());// Always null
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
This is rather old question that deserves an answer. This issue is likely a result of using FTPClient when secure connection is required. You may have to switch to FTPSClient if that is, indeed, the case. Further, output the response from the server with the following code snippet to troubleshoot the issue if secure client doesn't solve the it:
ftpClient.addProtocolCommandListener(
new PrintCommandListener(
new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));
Also, a server can reject your login attempt if your IP address is not white listed. So, being able to see the logs is imperative. The reason you see null when printing current working directory is because you are not logged in. Login method will not throw an exception but rather return a boolean value indicating if the operation succeeded. You are checking for success when changing a directory but not doing so when logging in.
boolean success = ftpClient.login(user, pass);
I faced the same, but I came across with a simple step.
Just added this.
boolean success = ftpClient.changeWorkingDirectory(dir);
ftpClient.printWorkingDirectory(); //add this line after changing the working directory
System.out.println(ftpClient.printWorkingDirectory()); //wont be getting null
Here I have the code and the console output
FTPClient.changeWorkingDirectory - Unknown parser type: "/Path" is current directory
I know I replied too soon ;-P, but I saw this post recently. Hope this helps to future searchers ;-)

Openshift: Trying to create java.websocket using JBoss Application Server

I am new to open shift, so bear with me. I have gotten open shift on my eclipse, and I have set up a JBoss Application Server 7. I want to make a server endpoint java class as described here. How do I do this? I noticed that, first of all, there is no java.websocket jar files in the open shift project that I created in eclipse. So I decided to import it and added to the build path. (This jar file I copied from glassfish's javax.websocket-api.jar). But whenever I would commit it and push my java class, it would give me a bunch of errors and mu open shift domain won't work.
So what am I doing wrong? Is it that the jar file that I imported is wrong (i.e. it isn't compatible with JBoss)?
Another thing that eclipse is telling me is that there is this error: "Faceted Project Problem (Java Version Mismatch) (1 item)"
Here is my server endpoint class:
package serverendpointdemo;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint("/serverendpointdemo")
public class ServerEndPointDemo {
#OnOpen
public void handleOpen () {
System.out.println("JAVA: Client is now connected...");
}
#OnMessage
public String handleMessage (String message) {
System.out.println("JAVA: Received from client: "+ message);
String replyMessage = "echo "+ message;
System.out.println("JAVA: Send to client: "+ replyMessage);
return replyMessage;
}
#OnClose
public void handleClose() {
System.out.println("JAVA: Client is now disconnected...");
}
#OnError
public void handleError (Throwable t) {
t.printStackTrace();
}
}
Thank you for your help!

External library not available when deploy with fat-jar

I'm using in my program the bluecove library.
While running the program via eclipse, all works smooth. I'm now trying to deploy my program, and following this post i'm using fat-jar.
When i run the jar file (created by fat-jar), the library can't be located, and i'm getting the exception BlueCove libraries not available as result of this line local = LocalDevice.getLocalDevice();.
In the fat-jar window i tried also to add bluecove-2.1.0.jar to the Class-Path place, and also with the path \src\JoJoServer\bluecove-2.1.0.jar.
I tried also to place the bluecove's jar file in different folders, such as the src, or an external folder.
Although i know it's not recommended, i tried the option of One-Jar, nevertheless it didn't help.
To run the jar (the one created by fat jar) i simply double click the file.
What i'm missing?
This is the entire code:
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
#Override
public void run() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
I have no clue what could be your problem, but I've tried the process and everything works, so just a summary of what I've did. Maybe you will figure it out by following it...
I don't understand how the posted code could be the entire, I see no class definition. :)
So I've modified it to the main method and it works both from the Eclipse and also by running the JAR generated by the FatJar.
The modified code of the BTTest class:
package test;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class BTTest {
public static void main(String[] args) throws Exception{
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString()
+ ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
}
To run or produce it, I have just put the bluecove library in the build path and created the fat jar with a simple way:
http://oi60.tinypic.com/vg1jpt.jpg
Starting the generated jar from command line:
D:\testProjects\bttest>java -jar bttest_fat.jar
BlueCove version 2.1.0 on winsock
0000110100001000800000805f9b34fb
BlueCove stack shutdown completed
Can you post a difference to your process?

How to do remote JNLP error reporting?

I have a JNLP downloader application deployed on remote user machines that downloads files.
I need to get some error feedback mailed to me. Not so much exceptions, just things getting stuck, or stalled or in infinite loops.
Currently I have a basic handler:
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class javaerrorlog {
private static Logger l = Logger.getLogger("");
public static void main(String args[]) throws Exception{
FileHandler handler = new FileHandler("log.txt");
l.addHandler(handler);
l.setLevel(Level.ALL);
l.info("Error logs");
try {
} catch (Error ex) {
l.log(Level.INFO, "", ex);
}
l.fine("");
}
}
Also, should I prompt for the client's permission to send error reporting data?
If you just need notifications you could use something like SMTPHandler. If you need it more fancy you could use JMS with an MDB.

Categories

Resources