Setting embedded Jetty war from classpath - java

How I can set a war in an embedded jetty in a way that it can load from a classpath. Following is my current code snippet
webAppContext.setWar("hello.war");
Context :-
I want to secure my code other than obfuscation.so, I used Jetty to create a runnable jar and subsequently i used winrun4j to create an exe wrapper. The exe works fine when war file is found at same level but not otherwise even though i've embedded the war in winrun4j exe.
Problem:-
Is there any way that i can set the war in a way that it can pick it up from classpath rather than some pre-defined path.
Hope i communicated the problem statement in a lucid way.
Thankyou.

I came around the same and I always extract the war to a temporary location and then use the absolute path;
webAppContext.setWar("/path/to/temp/tmp262622522.war");
In any case Jetty will extract the war to a temporary location too, when starting the web app.

Related

Running a Java Web Start application without Java Web Start

I want to run a fat client delivered as a Java web start application without Java web start. I launched it via javaws and managed to get all the jar files mentioned in the JNLP file from the cache after they were downloaded.
I tried running the jar file that contains the main class according to the JNLP file, but I get the 'Could not find or load main class' error. Were I just trying to run a class I'd set the classpath accordingly, but since I'm running a jar file with java -jar, as far as I know the classpath settings will be ignored anyway. Now I'm not sure what to do, does anybody know how to tackle this?
I'll answer this myself now, turns out it is stupidly simple: Get all the jar files, unzip them to get the content, merge all the content (best done with rsync), create a new MANIFEST.MF file that contains the main class to be loaded and the merged hashes for all existing files from all MANIFEST.MF files, zip again to create a jar. That's it.

How do I configure embedded jetty to used war file in executable jar as resource

I have an executable jar with a war file in it. Running the jar extracts the war file and creates a WebAppContext from it using webAppContext.setWar(warFile). Although that works, it seems that webAppContext.setWarResource(warResource) should work. I've tried it creating a resource using new PathResource("file.war") which shows a path like "jar:file:/Users/.../jetty-1.0.0-jar-with-dependencies.jar!/file.war". Sounds promising and conventional, but when I try it I get "java.lang.IllegalArgumentException: not file: scheme". Do I really have to extract the war file or is there a trick?
That would be a nested jar content reference and no Java program can do that.
Option 1: use a live-war (aka an executable-war) instead.
This would be a war file that can be deployed traditionally if you want to, but can also be used standalone from the java command line (and will start it's own server if it needs to).
An example project is maintained by the Eclipse Jetty project at ...
https://github.com/jetty-project/embedded-jetty-live-war
Note: the live-war concept was inspired by work done by the Jenkins project and their live-war.
Option 2: eliminate the WAR file layer entirely in your JAR
Don't package the WAR contents in your JAR as a filename.war, consider using it as an exploded WAR (or war directory) instead.
Just unpack the WAR into your JAR file somewhere safe (like /META-INF/webapps/<app-id>/) and then reference that directory location in your JAR file instead.
Option 3: eliminate the need for the WAR concepts entirely
This is the number one most popular choice.
You deconstruct your WAR file into a ServletContextHandler with configured Servlets and Filters, this also eliminates the need for things like annotation scanning / bytecode scanning (which is quite complicated), you'll also not have to wrangle the nested / isolate classloader (your uber JAR file contains all of the classes and downstream dependencies needed to run your webapp), and this approach will definitely speed up your startup time.

Eclipse showing incorrect project path

I have a maven web project in eclipse. I need to get the project's path, actually have to get list of files under src\main\resources\someFolder in project.
Tried String dataDir = "src\\main\\resources\\someFolder";, on running this directory structure is created inside eclipse folder like F:\softwares\eclipse\eclipse\src\main\resources\someFolder. Same when using / instead of \\.
Tried System.getProperty("user.dir")and new File(".").getAbsolutePath(), they return F:\softwares\eclipse\eclipse.
I need to access the project folder in my workspace F:\workspace\Project\src\main\resources\someFolder
But when created a core java app and used System.getProperty("user.dir")and new File(".").getAbsolutePath(), I am getting project path in workspace, F:\workspace\Project. This src\\main\\resources\\someFolder also works fine then.
Why this odd behavior from eclipse?
As mentioned here the directory user.dir is the place where the JVM is started. As web applications are mostly jar/war/ear packages placed somewhere within the folder of the server eclipse handles them in a different way because the behaviour of such a web application is different. You cannot expect to have file access from outside the jar/war/ear file. Within the jar/war/ear file everything from within src/main/resources will be available just by using getResourceAsStream as described in many other stackoverflow articles. This way you mustn't use src/main/resources/myfile.txt but myfile.txt.
Don't try to guess or use what the user.dir / JVM/server start folder is!

Does it matter how a war file is named?

Is it important how a war is called? Does it have any effect on whether it will work correctly? Let's say you're building a war with maven, naming it in one way and then manually rename it to some other name before deploying it somewhere on a server and running it, would it have any effect on working? Like deploy issues, etc?
Let's say with the maven war plugin you set it to generate something.war and later rename it to newfilename.war before deploying. What is your experience/knowledge there?
The name might matter depending on the servlet container: tomcat uses the name of the war file without the .war extension as the context path. This can be adjusted though using container-specific configuration files inside the .war file.
Obviously it would also matter in that case when you're trying to access it.

Play framework 1.2.4 war environemnt specific configuration file

Is there a way to include environment specific properties or configuration file while building war.
QA
entity.url=http://qa.test..
prod
entity.url=http://prod...
I need to make around 5 to 6 REST calls. Url is different for each environment. Hence is there any way to configure environment specific conf file?
thanks in advance
The Play Framework has the concept of 'ids' that can be used for different modes see here:
http://www.playframework.org/documentation/1.2.4/ids
This allows you to do:
%qa.entity.url=http://qa.test..
%prod.entity.url=http://qa.test..
The one thing that might not be clear by their documentation is how to set this in a war. When running as a .war file, the play ID is set to 'war' by default. This can be changed in the web.xml of the .war file. You can do that or you can specify the ID when you create the war:
play war -o PATH --%prod
Not that I am aware of (and reading the python source for building the war does not indicate this is available). The war file simply builds up your Play application, as is. If you want to have a different configuration, then this may simply require the loading of it from an external resource (a property file that lives outside of the WAR, that you ship with your WAR file).
Alternatively, you could modify the python script that builds the WAR file to custom add additional properties to your file. Look in the directory framework/pym/commands/ and look at the war.py to read the source for the python war command.

Categories