Browser can't connect to Jetty server - java

I saw another question like this on StackOverflow, but the answers he got were pretty, rediculous. "Connect to the localhost." -- Like, okay.
Anyway, the problem is I finally got my embedded Jetty server to compile and run, the problem was since I'm using it in an API i needed the sources, instead of just the dependency.
I'm running the most basic hello-world code right now, and the server is starting, but none of my browsers can form a connection.
Here's the code:
try {
httpServer = new Server(8080);
httpServer.setHandler(new JettyPage());
httpServer.start();
} catch(Exception e) {
e.printStackTrace();
}
Naturally, here's JettyPage.java:
public class JettyPage extends AbstractHandler {
#Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello world.</h1>");
}
}
The output in the console:
2014-11-07 07:01:05.155:INFO::main: Logging initialized #599ms
2014-11-07 07:01:05.190:INFO:oejs.Server:main: jetty-9.3.0.M1
2014-11-07 07:01:05.215:INFO:oejs.ServerConnector:main: Started ServerConnector#6e5e91e4{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2014-11-07 07:01:05.215:INFO:oejs.Server:main: Started #660ms
Firefox:
The connection was reset
Opera:
No data received
Chrome:
No data received
--- Yes, I am connecting to localhost:8080
I've also tried: 0.0.0.0:8080 and 127.0.0.1:8080
Note: If I disable my apache server that's running on port 80, and bind Jetty to port 80, I still can't connect.

I had exactly the same problem...very simple code, couldn't get browser to connect to Jetty even though it compiled and ran from both command-line and IDE. Finally traced it down to mismatched servlet and Jetty jars (at least I think that caused the problem). The ones that worked for me were javax-servlet-api-3.1.0.jar and jetty-all-9.2.8.v20150217.jar. Once I started using those I could connect if the code was compiled at command-line or from NetBeans IDE.
For raw noobs like me, most of the online examples for command-line compile use Linux/Unix path separators (":"). It took me several minutes to figure out on a Windows machine I needed a different one (";"):
javac -cp .;javax.servlet-api-3.1.0.jar;jetty-all-9.2.8.v20150217.jar HelloWorld.java
Hope that helps.

If you followed the HelloWorld example exactly, you would have a response.
There is still something wrong with your runtime environment.
Is this windows you are running on? If so, try setting the port to something other than 8080 (try 38080), as you might have something already on that port, and Windows will not throw an error or warning in this situation. (on other OS's this would result in a bind exception)
Do you have any logging output that indicates an error or warning?
There should be plenty of logging output to the console, even when there is an error in starting up the server, or serving some content.
If you want to enable VERY verbose logging, you can add -Dorg.eclipse.jetty.LEVEL=DEBUG to your java execution.
Windows Version
> java -Dorg.eclipse.jetty.LEVEL=DEBUG -cp .;servlet-api.jar;jetty-all.jar HelloWorld
Linux / OSX / Unix Version
$ java -Dorg.eclipse.jetty.LEVEL=DEBUG -cp .:servlet-api.jar:jetty-all.jar HelloWorld

Related

GlassFish: list-commands gets me "requested resource is not available."

Using asadmin --port 8080 multimode, I created and started a domain on the localhost; so far, everything is fine. Then I try list-commands, and I get: HTTP Status 404: The requested resource is not available. If I try list-commands --localonly it works fine. I completely took down my firewall to see if that was the problem, but that didn't fix it.
This wouldn't be such a big deal, since I can look up the commands elsewhere, but I'm getting the same error when I try to deploy a WAR file, and I'm guessing that both problems have the same cause (after all, deploy isn't listed under the local commands).
I'm using GlassFish 4.1

Metamap run local raise "Error when querying Prolog Server: Connection refused"

I follow this steps to run the Metamap Java API 2014 on Linux platform
(Main page from metamap)
After start Metamap server, I run some main class in Metamap JavaAPI pakages, but it raises Error when querying Prolog Server: Connection refused
Then I check if the Metamap server is running on port 8066 by using the command:
sudo netstat -tulpn | grep 8066
but I receive nothing.
Did anyone had the same problem before and knows solution for this?
After running skrmedpostctl (and the optional word sense diambiguation server wsdserverctl), you need to run the mmserver executable in order to use the Java API for Metamap. This can be run by running the command
./bin/mmserver{two-digit-year}
Also, be sure to include the two jar files for metamap and prolog-beans in your classpath (in your IDE, this can be done by adding a dependency to these jars).
The issue may reside with the timeout: when the timeout parameter is not specified for the MetaMapImpl, it is set to 0, so the instance interprets any latency as failure and reports an error.
Increase the timeout to a reasonable value (for me 500 msec was enough).
I was getting this error after getting error SPIO_E_TOO_MANY_OPEN_FILES and losing the connection to the mmserver. I was tryig to construct instances of MetaMapApiImpl in a loop and calling processCitationsFromString for thousands of times. Therefore, I was getting error SPIO_E_TOO_MANY_OPEN_FILES after around a hundred repeats.
What I did to fix this error was to simply disconnect the api at the end of the loop, by calling the function disconnect().

Possible causes can be invoking https when the application is not configured for security

I create web service
#WebService(serviceName = "DynamipsService2")
#Stateless()
public class DynamipsService2 {
#WebMethod(operationName = "StartSession")
public static String StartSession(#WebParam(name = "key") String key) {
try {
return "100-Session started";
} catch (Exception ex) {
return null;
}
}
}
I want to test but on the page
http://localhost:8080/DynamipsService2/DynamipsService2?Tester crash bug
Error generating artifacts for the
following WSDL
http://localhost:8080/DynamipsService2/DynamipsService2?WSDL
Possible causes can be invoking https
when the application is not configured
for security
I created other Web services in the same assembly and it works.
I just experienced this problem as well. The solution for me was to use my hostname rather than localhost in the URL for the Tester.
So in my case, the following, which is what NetBeans/Glassfish did by default when I clicked Test Web Service in the NetBeans UI, did not work:
http://localhost:8080/Calculator/Calculator?Tester
However, when I paste the following into a browser, it does work:
http://david-pc:8080/Calculator/Calculator?Tester
I couldn't figure out how to change the hostname that NetBeans uses for the built-in Test dialog (nor could I cut+paste the URL from the error dialog). So I had to visually copy the URL from the error message into a browser, replacing the hostname along the way.
I had the same problem and the reason apeared in the Server's log. I'm useing Glassfish 3.1 with netBeans 7. And the error I got in the Glassfish output was:
INFO: [ERROR] com.sun.tools.javac.Main is not available in the classpath, requires Suns JDK version 5.0 or latter.
I googled a bit and it appeared to be because the glassfish server was working with the openjdk that came with ubuntu. If your problem is the same the solution I found was to remove the openjdk jre, like this:
sudo apt-get remove openjdk-6-jre
sudo apt-get autoremove
Hope this is useful.
PS: I assigned /usr/lib/jvm/java-6-sun/bin/java in the java tab at the servers configuration wizard in netBeans but don't know if that was part of the solution (I'm afraid to change it back :p)
I had the same problem with Glassfish
but it was more compilcated because of the NAT
I have GF in NAT - do MYDOMAIN and port is redirected to internal machine
the problem in GF is that it tries to connect to itself by the domain name which again redirects to inside network
(the ?wsdl works properly)
I have made temp solution adding to /etc/hosts (127.0.0.1 domainname)
be aware that it's only a temp solution
try to check if you have "localhost" in your hosts file (in windows c:/windows/system32/drivers/etc/hosts )
and ping localhost
I will add GF log - maybe someone in future will search it by google :) :)
moreover I looked GF logs there was something like ->
Connection timed out
Failed to read the WSDL document: http://MYDOMAIN:8080/MYSERVICE?WSDL, because 1) could not find the document; /2) the document could not be read; 3) the root element of the document is not <wsdl:definitions>.
failed.noservice=Could not find wsdl:service in the provided WSDL(s):
At least one WSDL with at least one service definition needs to be provided.
Failed to parse the WSDL.
Which web server do you use? If you use glassfish you can open server admin page and select Configurations===>server-config===>Security
and make Security Manager enabled
Check your domains/{YOUR_DOMAIN}/config/domain.xml
If you setup Glassfish using Eclipse, all will be done for you automatically.
Now I am surprised if I start the domain from the command line, it gave me that error, but starting Glassfish 4 from Eclipse, it is not showing any problem.
One cause could be that you don't have correctly configured the environment variable JAVA_HOME (with the correct path) and the JAVA_HOME/bin directory added to global PATH environment variable as well. For some processes the glassfish look for the classpath of the JDK installed.
Hope this help.
I had the exact same problem and it is because you have a static method, I realised after debugging for some while. Just remove static from the method and it should work.
#WebMethod(operationName = "StartSession")
public String StartSession(#WebParam(name = "key") String key) {
try {
return "100-Session started";
} catch (Exception ex) {
return null;
}
}

Working with Php-Java Bridge

I am having trouble setting up the Php-Java Bridge setup properly.
I will explain what I have done.
My site is in pure php
For our payment transaction process we need to set up a php-java bridge
I followed this link to setup the bridge PHP-JAVA BRIDGE INSTALATION.
Here I learned that I need to have a private jvm to install the bridge.
So 1st i installed apache-tomcat-6.0.14 in Private JVM using my c-panel. After instalation it asked me to Map a domain to private JVM. So I mapped my domain example.com (which is the only option available) to it.
Then it asked to enable a traffic redirection from Apache web server to my Java application server (there was a check box and i clicked it)
Finally it asked me to deploy the WAR File (JavaBridge.WAR was my file) and everthing seems fine
Now when i go to http://example.com/JavaBridge/ I could see the javabridge examples and it works fine.
SO FAR SO GOOD
Now my problem starts here when I try to access a java class file from php. A sample test.php is what I create and put the following code into it.
<?php
require_once("http://example.com:portnumber/JavaBridge/java/Java.inc");
$System = java("java.lang.System");
echo $System->getProperties(); //This Part echo's correctly and shows the data so it means i can access Java.inc Correctly
$path_e24class = getcwd(). '/e24PaymentPipe.class'; //This part fails both test.php and java class file e24PaymentPipe.class are in the same directory in publich_html folder
java_require($path_e24class);
$pipe = new Java("e24PaymentPipe");
$pipe->setAction("1");
?>
My site contents reside in the public_html folder and the WAR file are deployed in private jvm.
These are the error message am getting.
1) Warning: java_require() not supported anymore. Please use tomcat or jee hot deployment instead
Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new e24PaymentPipe. Cause: java.lang.ClassNotFoundException: e24PaymentPipe VM: 1.6.0_22#http://java.sun.com/" at: #-10
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358) #-9
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204) #-8
java.lang.Class.forName0(Native Method) #-7
java.lang.Class.forName(Class.java:247) #-6
php.java.bridge.Util.classForName(Util.java:1518) #-5
php.java.bridge.JavaBridge.CreateObject(JavaBridge.java:445) #-4
php.java.bridge.Request.handleRequest(Request.java:458) #-3
php.java.bridge.Request.handleRequests(Request.java:500) #-2
php.java.bridge.http.ContextRunner.run(ContextRunner.java:145) #-1
php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:60) #0
http://example.com:portnumber/JavaBridge/java/Java.inc(232): java_ThrowExceptionProxyFactory->getProxy(3, 'java.util.Prope...', 'T', false) #1
Finally I don't know much about the java. So am stuck here not knowing what to do.
Here is a great step by step tutorial you can follow, which shows everything required! It is a little old (2007) but helped me a while ago.
There is also another option. You can install Apache Tomcat and deploy your war there. You can have even multiple tomcat instances simultaneously with your httpd running at the same time on the same machine, as long as you respect the port settings. You can even front them with Apache httpd.
you can try this:
package your code to jar, and copy it to java.ext.dirs which you can found in JavaBridge.log
copy the related class libraries to java.ext.dirs
restart the service of JavaBridge
good luck!
<?php require_once("JavaBridge/java/Java.inc");
try {
$hd = new java("hdfs.HDFS");
$hd->get("hdfs://master:9000/user/hadoop/test-in/logo_cn.png", "/home/hadoop/1.png");
} catch (JavaException $ex) { echo "An exception occured: "; echo $ex; echo "<br>\n";}
?>
You can use this php implementation on github that works with php 5.3.
See credits on the git readme for more information.
You can try this; put the JavaBridge.jar in tomcat's lib folder e.g. apache-tomcat-7.0.12/lib.
Restart tomcat server and then,
$pipe = new java("com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe");
$pipe->setAction("1");
This way I created the php version of the object.
Why don't you put the e24PaymentPipe class in your Java application's classpath and skip the two lines below:
// $path_e24class = getcwd(). '/e24PaymentPipe.class';
// java_require($path_e24class);
$pipe = new java("fully.qualified.classpath.e24PaymentPipe");
You are mixing PHP side and Java side operations. in theory the java_require (which is deprecated) was designed to work on the Java side. You are specifying a PHP side path.
You can save yourself a lot of grief by using a pure PHP implementation of the e24PaymentPipe library.
Disclaimer
The link is to my github repo of the library, but I did not write it. See the readme in for original credits.

maven jetty does not shutdown properly in eclipse

I am currently running jetty from eclipse as an external java program. The problem is when I terminate jetty and I try to relaunch it again, it cannot due to the port still being in use.
What I have to do is open up Windows Task Manager and kill the java process manually. How do you get jetty to shutdown/terminate/end nicely?
The following are my maven jetty application settings
Arguments: jetty:run-war
MAVEN_OPTS: -Xdebug -Xnoagent
-Djava.compiler=NONE -Xrunjdwp:transport=dt_socket, address=8080,server=y, suspend=y
Setting suspend=n doesn't seem to solve the problem either.
If a java application does not shutdown it is because of an alive non-daemon thread. Try getting a thread dump of the running maven process, e.g. using VisualVM and see what keeps the application alive.
You could run the application via the 'Run Jetty Run' Eclipse plugin, rather than the Maven jetty plugin. Eclipse has more direct control over the new JVM then.
I'm not familiar with the maven plug-in, but when starting jetty with start.jar, you can use stop.jar to have it shutdown gracefully (since version 4.something).
Stopping Jetty
Programs started with the start.jar mechanism may be stopped with the
stop.jar:
java -jar stop.jar
This connects via a local port to stop the server. The default port can be set
with the STOP.PORT system property
Source
Alternatively, and maybe more to your liking, you can do it within eclipse by writing a class like this, and running the main method:
package mypackage;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Stop {
public static final int PORT = 8079; //Change to whatever your port is, 8079 is default
public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getByName("127.0.0.1"), PORT);
OutputStream out = s.getOutputStream();
System.out.println("*** sending jetty stop request");
out.write(("\r\n").getBytes());
out.flush();
s.close();
}
}
Source
It seems that I forgot to tick "Allow termination of remote VM". Strange that it didn't seem to work before.

Categories