I have the following code:
Server server = new Server(9090);
final URL warUrl = Main.class.getClassLoader().getResource("com/domain/webapps/app");
final String warUrlString = warUrl.toExternalForm();
WebAppContext wac = new WebAppContext(warUrlString, "/app");
server.setHandler(wac);
I have the Main class in the com.domain package.
The jsp's and html's are in the com.domain.webapps.app package.
When run inside Netbeans (or java -cp <classpath> com.domain.Main on the exploded jar) the application works perfectly.
If i run the jar (java -jar app.jar), the content of the com.domain.webapps.app gets extracted to /tmp/Jetty_something/webapp/, so the full path is /tmp/Jetty_something/webapp/com/domain/webapps/app/
But when i make a request for http://localhost:9090/app/file.jsp, Jetty tries to get the file from /tmp/Jetty_something/webapp/file.jsp (the wrong place :-( )
What can i do where ?
Jetty version is 6.1.26
Have a look at this article. The URL is detected by
ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
This works for me in a war project and maybe also for your jar use case.
Related
I am trying to run runner jar of the quarkus application which would be listening over port 9411 on http.
Programmatically using UrlClassLoader, when I try to load the jar it throws
(also with java -jar)
1.java.lang.ClassNotFoundException: io.quarkus.runtime.Quarkus
2.java.lang.reflect.InvocationTargetException
here is the snippet of code ,
URLClassLoader loader = new URLClassLoader(
new URL[]{ new File(<location of runner jar>).toURI().toURL()});
Thread.currentThread().setContextClassLoader(loader);
Class<?> mainClass = loader.loadClass("io.quarkus.runner.GeneratedMain");
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.invoke(null, (Object) new String[]{});
another observation is when I place /lib folder at the runner jar location it loads successfully meaning it requires the lib folder altogether.
How can I make my code work only with runner jar?
To produce a fat jar that includes all the libraries necessary to run the app, use the property quarkus.package.uber-jar=true (you can add that into src/main/resources/application.properties or pass it as a system property when running the build).
With mvn clean package I got the following error starting:
Unrecognized configuration key "quarkus.package.uber-jar" was provided
I've found
quarkus.package.type=uber-jar
as a property.
What I prefer is setting
<quarkus.package.type>uber-jar</quarkus.package.type>
in the pom properties.
https://github.com/fluentcodes/sandbox/tree/java-quarkus-empty
We have a nested multi-module project. Our developers are a mix of IntelliJ IDEA and Eclipse users.
When running a jetty server inside an inner module, it seems we need to set the resource base to different values depending on which IDE we are using.
For IntelliJ:
root.setResourceBase("myModule/src/main/webapp");
For Eclipse:
root.setResourceBase("src/main/webapp");
We don't want to have to tweak our IDEs to make it work, e.g. I don't want to have to change some setting in IntelliJ to make it work with the Eclipse version of the code.
Any ideas?
The short answer:
Your execution differences between Eclipse vs Intellij can be explained by having different PWD, or ${user.dir}, or working directory setups.
The better answer:
Don't use filesystem paths then.
Look up a known resource in that location via a Classloader.getResource() and then pass the parent directory into the root.setResourceBase()
Example:
Server server = new Server(8080);
// Figure out what path to serve content from
ClassLoader cl = WebAppContextFromClasspath.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("hello.html");
if (f == null)
{
throw new RuntimeException("Unable to find resource directory");
}
// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(webRootUri.toASCIIString());
webapp.setParentLoaderPriority(true);
server.setHandler(webapp);
server.start();
server.join();
You can see this in the embedded-jetty-cookbook examples:
WebAppContextFromClasspath.java
ResourceHandlerFromClasspath.java
The other better answer:
Another approach is to find the src/main/webapp a few different ways depending on how it is being run
See the operational modes in the ServerMain.java in the embedded-jetty-live-war example.
I'm trying to load/read property files on glassfish 3.1 server.
I can't get it to work. I've searched and tried many possible solutions.
None worked so far, always NULL as result.
I've tried methods from the following link:
How to use a property with Glassfish
My method:
final public class GTS_Properties{
...
public static Properties getPropertiesFromFile(String fileName){
URL url = GTS_Properties.class.getResource(fileName)
...
// url is always null
}
}
My configuration:
Folder properties on build path.
/root/WEB-INF/classes/****.properties
How the Glassfish server looks like (using eclipse startup plugin)
/glassfish-root/domain/eclipseApps/MyWebApp/WEB-INF/classes/****.properties
I have tried to put the property files directly in WEB-INF instead of classes. same result.
I solved it by first going 2 'parent folders up' from the startpoint of the relative path.
Inserting "../../" on the start of the path was all that was needed.
WEB-INF/classes/WebApp/utils/GTS_Properties.class (The class to get the resource from)
WEB-INF/classes/prop.properties (Location of the resource file)
GTS_Properties.class.getResource("../../prop.properties")
I want to get tomcat's installation directory in my computer using java. I tried using :
System.getProperty("catalina.base");
and
System.getProperty("catalina.home");
But both methods return null as the answer. I tried it with System.getProperty("java.home"); and it correctly returns the java path.
Any ideas as to what the problem is? Thanks
Try installing this JSP and passing various values for the "property" parameter:
<%
String propertyName = request.getParameter("property");
Object propertyValue;
String typeString;
if(null == propertyName)
propertyValue = null;
else
propertyValue = System.getProperty(propertyName);
if(null == propertyValue)
typeString = "null";
else
typeString = propertyValue.getClass().getName();
%>
The system property <code><%= propertyName %></code> has the value:
<code><%= propertyValue %></code> (<%= typeString %>).
Maybe you can find a pattern to which property values return null.
I also encountered the same error as you did. but then I realized that the tomcat server was not running. After starting the server, I still failed to get the tomcat path.
Then I realized that I was trying to get the path from the main method in a JSF project while tomcat was still running.
So i finally got the tomcat path by using: System.out.println(System.getProperty("catalina.base")); in a #PostConstruct method of one of my #ViewScoped beans in a JSF project. And it successfully displayed C:\Documents and Settings\S!LENT W#RRIOR\Application Data\NetBeans\7.2.1\apache-tomcat-7.0.27.0_base in the console in NetBeans.
So In order to get Tomcat's installation directory, following points should be kept in mind:
Tomcat Server is running
You're not trying to get path from main method
hope this helps
we are using cucumber for testing and in cucumber cases, we are accessing project's individual method. in project, we are reading configuration for xml and the location is in config folder of Apache tomcat. Now when i run project,System.out.println(System.getProperty("catalina.base")); it's provide the proper path however when i call project's method from cucumber then it's returning null
I created a Netbeans Platform Application using Netbeans 7.0.1 and the JDK 1.7.
I implemented my own Web Application on a normal module using Embedded Jetty 7.4.5 (consisting of a Web Service and a couple of servlets), and I created a Library Wrapper Module including all the Jetty jar files and the "jetty-j2sehttpspi-7.4.5.v20110725.jar" that I needed to be able to publish the Web Service's Endpoint. The Web module has a dependency on the Jetty module.
The code I'm using is this:
System.setProperty("com.sun.net.httpserver.HttpServerProvider",
"org.mortbay.jetty.j2sehttpspi.JettyHttpServerProvider");
server = new Server();
JettyHttpServerProvider.setServer(server);
//We read the config file
String[] configFiles = {"etc/jetty.xml"};
for(String configFile : configFiles) {
XmlConfiguration configuration =
new XmlConfiguration(new File(configFile).toURI().toURL());
configuration.configure(server);
}
// Web Services
QueryWeb qWS = new QueryWeb();
Endpoint.publish("http://0.0.0.0:" +
(server.getConnectors()[0].getPort()) + "/ws", qWS);
// Servlets
HandlerCollection hc = (HandlerCollection)server.getHandler();
ServletContextHandler sch =
new ServletContextHandler(ServletContextHandler.SESSIONS);
sch.setContextPath("/web");
sch.addServlet(stream.class, "/stream");
// We add the servlet handler to the server's context handler collection
// because it's the one used by the Web Service Endpoint
ContextHandlerCollection chc = (ContextHandlerCollection)hc.getHandlers()[0];
chc.addHandler(sch);
server.start();
When I try and run the application, I get the following error after the "Endpoint.publish" call:
Exiting C:\Program Files (x86)\NetBeans 7.0\harness\run.xml.
Exiting C:\Program Files (x86)\NetBeans 7.0\harness\run.xml.
C:\Program Files (x86)\NetBeans 7.0\harness\suite.xml:500:
The following error occurred while executing this line:
C:\Program Files (x86)\NetBeans 7.0\harness\run.xml:225:
The following error occurred while executing this line:
C:\Program Files (x86)\NetBeans 7.0\harness\run.xml:193:
The application is already running within the test user directory.
You must shut it down before trying to run it again.
As far as I understand, this is happening because the system can't find the "org.mortbay.jetty.j2sehttpspi.JettyHttpServerProvider" class. Therefore it defaults back to the web server included in the JDK, which causes a conflict since we get both web Servers (Jetty and the JDK's) trying to run on the same port (in this case it's 8081).
The only way I managed to fix this problem was by copying all the Jetty jar files into the JRE's "lib/ext" folder (copying only the "jetty-j2sehttpspi-7.4.5.v20110725.jar" results in no errors, but the server won't start). In this way the system can find the class it needs and all it's dependencies.
I suppose that what's going on is that even if NetBeans uses it's own classpath loader, the System.setProperty method is ignoring this and trying to access the standard classpath, and since a NetBeans Platform Application doesn't actually let you change the classpath directly (that would beat the whole purpose of having modules administered by the NetBeans platform), I don't really know how to make it use the library included in the wrapper module.
I can keep developing the application with the temporary solution I found, but honestly, copying stuff into the JRE folders is not an acceptable solution and will eventually result in distribution and installation problems in client machines (already tried it in a Mac OS machine and I didn't even know where the JRE kept it's libraries to try and do the same dirty trick).
Therefore I want to ask you guys if there is any solution to this particular problem or if anyone has a better explanation of what's going on and how I might fix it without having to recreate the whole architecture of my project (which actually works OK except for this little inconvenient).
Thanks in advance!
Write your question to the mailing list, dev#platform.netbeans.org, and you're more likely to get an answer.