Using Jetty in Eclipse Juno - eclipse

As I understand, Eclipse Juno release has Jetty servlet container embedded. So I have assumed, that means I can use Jetty in Eclipse Java EE without installing any additional plugins or packages. I have tried this:
Created a simple dynamic Web project and run it on "J2EE Preview" Server. As a result I've got 404 error:
Error 404 - Not Found
No context on this server matched or handled this request. Contexts
known to this server are: ServletTest(/ServletTest)
Tried to create embedded Jetty instance:
public class Launcher {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
server.start();
server.join();
}
}
I am unable to find where can I import org.eclipse.jetty.server.Server class from.
What am I doing wrong?

Stick with your first approach. Looks like you're nearly there - you're just pinging the wrong URL. Try http://localhost:8080/ServletTest.

There are some bugs currently in the J2EE preview which uses jetty. It seems that at this moment the Run-Jetty plugin is simply better imprelemnted...
http://code.google.com/p/run-jetty-run/
If work perfectly for my web application, just using run as run-jetty

Related

intermittent "No main class is found." errors from the Spring Boot Dashboard in visual studio code

I'm just getting started with Java and I prefer Visual Studio Code over Eclipse. I downloaded the popular Java extensions for it including the Spring Boot tools (including Dashboard). When I switch to the Spring Boot Dashboard and click the Run or Debug button, I sometimes get a "No main class is found." popup error message and nothing starts. If I restart vscode, sometimes it works with the project that previously didn't, but another project that worked in the past is now throwing this error. It doesn't always happen with the same project. It all seems very random about when this exception is thrown. I have about 6 microservices projects (Eureka discovery, gateway, config server, different business logic), and it's hard to get all 6 to start. I can reliably start them with the default Run and Debug option or via jars - and they all always work.
All the projects were created with Spring Initializr and they do indeed have a main function:
package ...;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
public class ...ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(...ApiGatewayApplication.class, args);
}
}
This is a known issue with Vscode :
https://github.com/microsoft/vscode-java-debug/issues/606
And the workaround solution is, install Code Runner Extension :
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
then, click Run icon in the upper right-hand corner,press F1, Clean the java language server workspace.

Jersey REST get returns java.lang.NoSuchMethodError

I'm trying to get a response from a working REST webservice.
I have come up with the following looking at the documentation:
void postRest()
{
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://localhost:8080/TestApplication/");
WebTarget resourceWebTarget = webTarget.path("webresources");
WebTarget peopleWebTarget = resourceWebTarget.path("people/get/all");
Invocation invocation = peopleWebTarget.request("text/xml").header("Content-Type", "application/xml").buildGet();
String response = invocation.invoke(String.class);
System.out.println(response);
}
However I am returned a not very friendly:
Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.jersey.message.internal.MessagingBinders$MessageBodyProviders.<init>(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;)V
at org.glassfish.jersey.client.ClientBinder.configure(ClientBinder.java:118)
at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:169)
at org.glassfish.jersey.internal.inject.Injections.bind(Injections.java:157)
at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:147)
at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:137)
at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:352)
at org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:85)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:117)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:114)
at org.glassfish.jersey.internal.util.collection.Values$LazyValue.get(Values.java:275)
at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:669)
at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:276)
at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:124)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:97)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$Builder.buildGet(JerseyInvocation.java:201)
at org.glassfish.jersey.client.JerseyInvocation$Builder.buildGet(JerseyInvocation.java:154)
at client.RestClient.postRest(RestClient.java:24)
at client.RestClient.main(RestClient.java:14)
Java Result: 1
I'm using NetBeans and didn't add any library as it seems the GlassFish Server comes with
javax.ws.rs-api.jar which make the above run.
Do I need to add Jersey.jar as well inside libraries or am I missing something?
Thank you
Seems Jersey developers are very maven-centric and discourage including the jars manually, so if you're not using maven but instead are including jars inside NetBeans you'll need to add the whole content of jaxrs-ri, including jars inside ext/ directory for the above code to run.
On a side note if you're using client and server side of jersey inside the same project the manual inclusion of jars may break he server side, so better keep them separate!
I had this problem while trying to run the above code in Eclipse.
1) The first problem was that org.jboss.resteasy.jaxrs-api was included, which solved the compile problems but not the runtime problems, giving the exception noted above. The solution to this problem is simply to include org.jboss.resteasy.rest-client into your pom.
2) A second problem was that I was started getting a java.lang.NoSuchMethodError: org.jboss.resteasy.spi.ResteasyProviderFactory.<init>(Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)V exception. The problem was that in Eclipse I had defined jBoss EAP version 6.3 in the servers, and even though the project wasn't using it, the jars were still being included into the classpath, causing a conflict during runtime.
The solution then is either to open a new workspace or a new copy of Eclipse that doesn't have a the jBoss EAP server defined, or to update the resteasy modules in your current jBoss EAP 6.3 server. This is the ultimate recommendation in this thread: RESTEasy Client + NoSuchMethodError. Makes sense to me because I would rather my rest-server be running under the current jars anyway.

Debugging servlets from Eclipse

In Eclipse I can select a servlet from a web application, and choose "Run As -> Run on Server" from the context menu.
One minor problem is that a browser is opened with a wrong url.
For example, I have written a servlet similar to:
package mypackage;
#WebServlet("/append")
public class AppendTest extends HttpServlet {
#Override public void doGet(HttpServletRequest request, HttpServletResponse response) ...
...
}
then I can test it in a browser with a URL similar to:
http://127.0.0.1:8080/mywebapp/append
But when I choose to run the servlet as explained above, the browser is directed to:
http://127.0.0.1:8080/mywebapp/servlet/mypackage/AppendTest
Although this is a minor problem for testing the application, in fact this issue does not let me debug it, since when I select "Debug As -> Debug on Server", the same problem happens, and the debugger stops after the 404 http status answer (if I correct the URL after the 404 status, the debugger is not active anymore).
The servlet container I am using is JBoss.
Thanks for any help.
UPDATE:
I was not able to debug since the server was not correctly configured in Eclipse (configured as managed externally instead of by Eclipse, thanks for the hint #CarlosGavidia).
This apparently solves the biggest problem. If someone knows how to make Eclipse to open the right URL of the servlet at once instead of myself annoyingly correcting the url every time I run/debug it I will be thankful.
I am starting to think that this is an Eclipse bug. If I explicitly declare the URL pattern in the web.xml then eclipse will use the correct url. That means that the value of the WebServlet annotation indicating that URL when writing the servlet class is just ignored by eclipse when running/debugging the application :(

OSGi - running outside eclipse

I have been working on getting my OSGi application to run outside eclipse. It has certainly wasn't as easy as I thought out would be certainly not just a case of running java -jar org.eclipse.osgi_3.7.1.jar and installing the plugins.
I'm hoping this is the last problem
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
This works fine in Eclipse. I have a persistence bundle and several fragmentation bundles that I load depending on the database type that I require. I then also have another bundle with the database JDBC drivers. In eclipse I can run it, out side eclipse I have been trying everything including using class loaders to load the Driver
Activator.context = bundleContext;
Class.forName("com.mysql.jdbc.Driver");
try {
context.getBundle().loadClass("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Any idea what to look at? From what I can see my setups are identical and both have the same bundles running.
Does your bundle import the package com.mysql.jdbc?
Read the following wiki page for some background on why this might work inside Eclipse but not in a proper OSGi environment: http://wiki.osgi.org/wiki/Why_does_Eclipse_find_javax.swing_but_not_Felix%3F

Resources injection doesn't work when running jms examples in eclipse.

I want to run the jms tutorial files in eclipse. I have configured glassfish properly and added the jms resources. It works ok if I run it in netbeans.
I created an "application client project" in eclipse.
Here is where the resources get injected:
#Resource(mappedName = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
#Resource(mappedName = "jms/Queue")
private static Queue queue;
#Resource(mappedName = "jms/Topic")
private static Topic topic;
Then, when it gets here:
connection = connectionFactory.createConnection();
I get a NullPointerException. For some reason it doesn't get the resources from the server so they are null.
It works if I build the project in eclipse and then run it from a command line using appclient:
appclient Producer topic 4
It says here http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jst.j2ee.doc.user%2Ftopics%2Fcjappcliproj.html that:
An application client module is used to contain a full-function client
Java™ application (non Web-based) that connects to and uses the Java
EE resources defined in your server. When you place the client code in
an application client module instead of a simple JAR file, the
application client benefits from the server's resources (it does not
need to re-specify the class path to Java EE and server JAR files) as
well as from easier JNDI lookup (the client container fills in the
initial context and other parameters).
So it seems that I am on the right track, but maybe I am missing some eclipse configuration.
As you've already noticed, if you run your Application in standalone mode you will not be able to access servers resources, this is by design. You need to use Glassfish appclient (or similar tools for other application servers) in order to run a Java EE Client App.
Heres another thread that may help you configure your project to run correctly: eclipse howto start a application client on java ee glassfish appl srv.