I'm new to GWT. I'm trying to connect my Oracle10g database to GWT server program,
I'm using this code:
public class Database_connect
{
public static void connect()
{
System.out.println("This is a test project");
Connection con=null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Oracle JDBC driver loaded ok.");
con=DriverManager.getConnection("jdbc:oracle:thin:#127.0.0.1:1521:XE","naren", "naren");
System.out.println("Connected with #localhost:1521:XE.");
System.out.println("We have done it successfully");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
in server file i'm calling the connect() function
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService
{
public String greetServer(String input) throws IllegalArgumentException
{
Database_connect.connect();
}
}
but it's showing error.
I don't know what to do now. Anyone help me. Thanks for advance.
the error is
[WARN] failed Server#6e058516
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at org.mortbay.jetty.nio.SelectChannelConnector.open(SelectChannelConnector.java:205)
at org.mortbay.jetty.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:304)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at org.mortbay.jetty.Server.doStart(Server.java:233)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)
at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1093)
at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:836)
at com.google.gwt.dev.DevMode.main(DevMode.java:311)
[ERROR] shell failed in doStartupServer method
You are running the project 2 times i think ..please kill all the java processes in task manager and run the project only once ..
Related
I have two computers. On one of them, is running an RMI Registry - which was created from this code alone:
package main;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class TheRegistry{
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.createRegistry(2020);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
System.out.println("Registry Created");
Scanner input = new Scanner(System.in);
input.nextInt();
System.exit(0);
}
}
}
The other computer has a server that is trying to register an Object on this registry, however, it gets an exception. Here is the code for the server:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
public class TextScramblerServer implements TextScramblerInterface
{
private static Remote obj;
// main method to export
#Override //Return input text as-is.
public String testInputText(String inputText) {
return "Your input text is: " + inputText;
}
#Override //Return the string reversed.
public String reverse(String inputText) {
String reversedInput = "";
for(int i=0; i<inputText.length();i++)
{
reversedInput=reversedInput+inputText.charAt((inputText.length()-1)-i);
}
return "Result: "+reversedInput;
}
#Override //Return the string scrambled.
public String scramble(String inputText) {
String scrambledInput="";
for(int i=0; i<inputText.length();i++)
{
if(i%2==0)
{
scrambledInput=scrambledInput+inputText.charAt(i);
}
else
{
scrambledInput=inputText.charAt(i)+scrambledInput;
}
}
return "Result: "+scrambledInput;
}
public void exportServer() throws Exception {
System.setSecurityManager(new RMISecurityManager());
obj = UnicastRemoteObject.exportObject(this, 2022);
Registry registry = LocateRegistry.getRegistry("132.205.94.50", 2020);
registry.bind("test", obj);
}
public static void main(String[] args) {
try {
(new TextScramblerServer()).exportServer();
System.out.println("Server is up and running");
}
catch(Exception e){
e.printStackTrace();
try {
UnicastRemoteObject.unexportObject(obj, true); //close port
} catch (NoSuchObjectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
I keep getting the error:
java.rmi.ConnectException: Connection refused to host: 132.205.94.50; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at TextScramblerServer.exportServer(TextScramblerServer.java:57)
at TextScramblerServer.main(TextScramblerServer.java:62)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 7 more
java.rmi.NoSuchObjectException: object not exported
at sun.rmi.transport.ObjectTable.unexportObject(Unknown Source)
at java.rmi.server.UnicastRemoteObject.unexportObject(Unknown Source)
at TextScramblerServer.main(TextScramblerServer.java:68)
I can't figure out why this is happening. I think I've tried everything
I ran your code and it worked for me after configuring the security policy.
Your ConnectionRefused exception means that the underlying TCP connection cannot be established. It's network issue, not an RMI issue.
Try running both the server and registry on the same host, and use localhost as the hostname. If it works, the problem is likely a firewall issue between the two hosts.
You can do a simple test of a TCP connection to the specific port using telnet. If the port isn't listening, telnet will give you a similar connection refused message. If the port is listening, you'll get something like this on the terminal:
Connected to localhost.
Escape character is '^]'.
Control-C to get out of the session.
The specific telnet output may vary based on your OS, but they are all about the same.
If it is a firewall issue, you'll have to open up the ports. How to do that depends on OS, but it's easy to find.
Either your Registry has been garbage-collected, you got the IP address wrong, or it is a public IP address and you haven't configured port forwarding.
You need to store the Registry reference in a static object to overcome garbage collection, although what the point of that program is when rmiregistry.exe already exists escapes me completely.
You're barking up the wrong tree anyway. You can only bind to an RMI Registry that is running in the local host. There is therefore never any need to use a Registry hostname other than "localhost" when binding or unbinding.
The reason you got the NoSuchObjectException is that you are trying to unexport the stub, which is referred to by obj, which is the result of UnicastRemoteObject.exportObject(), which returns the stub. See the Javadoc. You need to save the result of new TextScramblerServer() and unexport that.
I'm trying to connect to HSQL using java. I'm using a tutorial to work with JDBC and hibernate. It's the lynda tutorials. Anyway, here's the code below:
package com.lynda.javatraining.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
private static final String USERNAME = "dbuser";
private static final String PASSWORD = "dbpassword";
private static final String CONN_STRING =
"jdbc:hsqldb://data/explorecalifornia";
public static void main(String[] args) throws SQLException {
//Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
System.out.println("A");
System.out.println(conn == null);
System.out.println(CONN_STRING);
try {
System.out.println("B1");
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("B1-2");
System.out.println("Connected!");
} catch (SQLException e) {
System.out.println("B2");
System.err.println(e);
} finally {
System.out.println("B3");
if (conn != null) {
conn.close();
}
}
System.out.println("C");
}
}
Here's the error I'm getting:
A
true
jdbc:hsqldb://data/explorecalifornia
B1
2014-06-27T15:26:27.430-0400 SEVERE could not reopen database
org.hsqldb.HsqlException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile#76d682a[file =/data/explorecalifornia.lck, exists=false, locked=false, valid=false, ] method: openRAF reason: java.io.FileNotFoundException: /data/explorecalifornia.lck (No such file or directory)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.persist.LockFile.newLockFileLock(Unknown Source)
at org.hsqldb.persist.Logger.acquireLock(Unknown Source)
at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.lynda.javatraining.db.Main.main(Main.java:24)
B2
java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile#76d682a[file =/data/explorecalifornia.lck, exists=false, locked=false, valid=false, ] method: openRAF reason: java.io.FileNotFoundException: /data/explorecalifornia.lck (No such file or directory)
B3
C
Can anyone tell e what I"m doing wrong? Thanks.
A database has a lock file explorecalifornia.lck that prevents communication. You should delete lock file and restart the database. This may happens from time to time when you accidentally shutdown the database or system.
See the syntax of the command used from the command line to invoke shutdown. There's also an option how to shutdown server in Java.
At the beginning I had an error java.lang.ClassNotFoundException. I solved this problem by adding jars to \war\WEB-INF\lib folder. But then I got java.lang.NoClassDefFoundError: Could not initialize class com.orientechnologies.orient.core.Orient error while applying to database. This is the server code:
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
RegBugs rb = new RegBugs();
String a = rb.getDbAdres();
try {
//The error is here
ArrayList<String> table = rb.getAllGitRecords();
} catch (ParseException e) {
e.printStackTrace();
}
return a;
}
Compilation is finished successfully. And when I start to call to server I get An error occurred while attempting to contact the server. Please check your network connection and try again. from gwt. How could I solve this problem?
P.S. I use eclipse and gwt-plugin for it
The error occurs when I try to interrupt with orient database.
Trace:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.orientechnologies.orient.core.Orient
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
at com.orientechnologies.orient.core.db.raw.ODatabaseRaw.open(ODatabaseRaw.java:108)
at com.orientechnologies.orient.core.db.ODatabaseWrapperAbstract.open(ODatabaseWrapperAbstract.java:53)
at com.orientechnologies.orient.core.db.record.ODatabaseRecordAbstract.open(ODatabaseRecordAbstract.java:127)
at com.orientechnologies.orient.core.db.ODatabaseWrapperAbstract.open(ODatabaseWrapperAbstract.java:53)
at ru.sersem.testgwt.server.RegBugs.getBugRecordsCount(RegBugs.java:192)
at ru.sersem.testgwt.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 40 more
I'm fairly new to gwt, and figuring out the connections with a mysql database has me stumped. Since there are very few direct tutorials, I've been going off another stackoverflow question here.
http://stackoverflow.com/questions/8335322/java-gwt-mysql-connection-refused/8388422#8388422
Although I can't get it right. Few things, this project isn't using the GAE, just the GWT, as was suggested as a previous answer in the other question. Yes, I can connect to my database through another sample program, so the link to the database is open. I also imported my mysql driver to /WEB-INF/lib, as well as added it to my java build path.
The crux of this is, I don't know why I can't connect, and my console is useless, if anyone can see right off the bat what I'm doing wrong that would be fantastic, or if there was a way to print out more of the error message that would be great as well as I don't know how to view the console for the server side resources (I read somewhere that there might be more to the errors then what is shown? ) thanks.
here is my GreetingServiceImpl.java relevant code
private final Connection connect() {
String driver = "com.mysql.jdbc.Driver";
String dblink = "jdbc:mysql://localhost:3306/";
String dbname = "gwttest";
String dbuser = "user";
String dbpass = "test";
try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(dblink + dbname, dbuser, dbpass);
} catch (SQLException e) {
System.err.println("mysql connection error: ");
e.printStackTrace();
}
return conn;
}
here is my helloserver.java relevant code
Button b = new Button("test");
vPanel.add(b);
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
GreetingServiceAsync testservice= (GreetingServiceAsync) GWT.create(GreetingService.class);
testservice.echo("test", new AsyncCallback<String>() {
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
vPanel.add(new Label("error"));
//vPanel.add(new Label(caught.printStackTrace());
caught.printStackTrace();
}
#Override
public void onSuccess(String result) {
// TODO Auto-generated method stub
vPanel.add(new Label(result));
}
});
}
});
here is the error message I've recieved upon running and clicking the button (besides the "error" which pops up in my html)
com.google.gwt.user.client.rpc.StatusCodeException: 404 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
<p>RequestURI=/helloserver/greet</p><p><i><small>Powered by Jetty://</small></i></p><br/>
</body>
</html>
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:209)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
The error is indicating a problem with the RPC. Database connection problems should cause an exception to be serialized to the client, not give a 404. Check that the web.xml and servlet are configured correctly. See: https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC.
If there were any JDBC errors, then they should be visible in the DevMode window, or in the server's log directory if running on a server. It looks like the GreetingServiceImpl class isn't reached at all though so there won't see anything there.
It may be easier to extract the JDBC code to a separate class, and test it separately from GWT. Get one thing working before combining everything together. Run it via a main method, or JUnit test, then you know whether the problem is with the JDBC code or elsewhere.
I've been using RMI in this project for a while. I've gotten the client program to connect (amongst other things) to the server when running it over my LAN, however when running it over the internet I'm running into the following exception:
java.rmi.ConnectException: Connection refused to host: (private IP of host machine); nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy1.ping(Unknown Source)
at client.Launcher$PingLabel.runPing(Launcher.java:366)
at client.Launcher$PingLabel.<init>(Launcher.java:353)
at client.Launcher.setupContentPane(Launcher.java:112)
at client.Launcher.<init>(Launcher.java:99)
at client.Launcher.main(Launcher.java:59)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 12 more
This error is remeniscent of my early implementation of RMI and I can obtain the error verbatum if I run the client locally without the server program running as well. To me Connection Timed Out means a problem with the server's response.
Here's the client initiation:
public static void main(String[] args)
{
try
{
String host = "<WAN IP>";
Registry registry = LocateRegistry.getRegistry(host, 1099);
Login lstub = (Login) registry.lookup("Login Server");
Information istub = (Information) registry.lookup("Game Server");
new Launcher(istub, lstub);
}
catch (RemoteException e)
{
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
catch (NotBoundException e)
{
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
Interestingly enough no Remote Exception is thrown here.
Here's the server initiation:
public static void main(String args[])
{
try
{
GameServer gobj = new GameServer();
Information gstub = (Information) UnicastRemoteObject.exportObject(
gobj, 1099);
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("Game Server", gstub);
LoginServer lobj = new LoginServer(gobj);
Login lstub = (Login) UnicastRemoteObject.exportObject(lobj, 7099);
// Bind the remote object's stub in the registry
registry.bind("Login Server", lstub);
System.out.println("Server ready");
}
catch (Exception e)
{
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
Bad practice with the catch(Exception e) I know but bear with me.
Up to this stage I know it works fine over the LAN, here's where the exception occurs over the WAN and is the first place a method in the server is called:
private class PingLabel extends JLabel
{
private static final long serialVersionUID = 1L;
public PingLabel()
{
super("");
runPing();
}
public void setText(String text)
{
super.setText("Ping: " + text + "ms");
}
public void runPing()
{
try
{
PingThread pt = new PingThread();
gameServer.ping();
pt.setRecieved(true);
setText("" + pt.getTime());
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
That's a label placed on the launcher as a ping test. the method ping(), in gameserver does nothing, as in is a null method.
It's worth noting also that ports 1099 and 7099 are forwarded to the server machine (which should be obvious from the stack trace).
Can anyone see anyting I'm missing/doing wrong? If you need any more information just ask.
EDIT: I'm practically certain the problem has nothing to do with my router settings. When disabling my port forwarding settings I get a slightly different error:
Client exception: java.rmi.ConnectException: Connection refused to host: (-WAN IP NOT LOCAL IP-);
but it appears both on the machine locally connected to the server and on the remote machine.
In addition, I got it to work seamlessly when connecting the server straight tho the modem (cutting out the router. I can only conclude the problem is in my router's settings but can't see where (I've checked and double checked the port forwarding page). That's the only answer i can come up with.
Are you using NAT (Network Address Translation)? This is the typical case if your LAN uses non-routable address behind a router (like 10.x or 192.169.x etc).
If this is the case, you need to specify the public IP of the server with following option,
-Djava.rmi.server.hostname=host_name_or_public_ip