How to sign JNLP file? - java

I'm having a Java 7 application that's being built using ANT script (application is built into a jar file) and deployed to clients using JNLP file.
Currently I need to put some "property" into JNLP file but that property isn't visible from within my app unless I mark that particular property as "secure" (meaning to put "jnlp." as property name prefix). That works but I don't think it's a good way to provide my custom properties to the application.
In order to use jnlp properties they way I should, I have to "sign" jnlp file as described here.
As I'm using Java 7, the ability to make JNLP template is nice but I'm not sure were to put described JNLP template in my project structure nor how to include it in my Ant build.
I'm not very "fluent" with Ant.

You need to use the signJar task in Ant. Here is a link to the documentation and examples of how to use it.
https://ant.apache.org/manual/Tasks/signjar.html

You need to add the JNLP to the JAR file under the directory and name specified in your link, which is done via the <jar> task, and then sign it with the <signjar> task.

As the article describes if you want to sign your JNLP file you have to put it into the JNLP-INF subdirectory. So your project structure should look like this:
YourApp.jar:
\- com (folder with your java classes)
\- META-INF (folder which contains the manifest)
\- JNLP-INF (folder which contains your.jnlp file)
\- ... (other folders like res, dtd etc.)
The JAR file is created and signed in the usual manner - there is no extra task necessary. Please note, that when the app is started, the JNLP file used must be identical to the JNLP file in the signed JAR in order for the application to run.
under "jar" i have only 2 subnodes: "manifest" and fileset dir="${workspace.path}${project.name}/bin"
This means that a manifest will be created and every file and folder which is in the /bin directory will be included in your app.jar file. So e.g. eclipse automatically puts each of the source directories into the /bin file. So depending on what kind of IDE you are using / or not using you have to copy the JNLP-INF manually. Since signing is always done on the whole jar file you doesn't need to do anything...

Related

Deployed spring boot jar file to Docker - how to access and change static resources

I'm kinda new to spring and web development as a whole.
My question is:
When you build a spring boot project (using Maven) into jar file and deploy it via Docker, everything is in one jar file. How can you access your resources (css, js, images, html...) if you want to edit something? Like change something in css file or add something to html page. Is it even possible? Or do you have to build a new jar file everytime, when you need to change something (in frontend)? Also, when there are being uploaded some images or other files, where are they stored? This stuff is very confusing for me and i can't find any related books or help at all.
Thanks for help!
when you package any java program it is nothing but a zip file. Based on what kind of package it is, you wither name it as a Jar or War.
Jar == Java archive
War == Web archive
Now, given the fact that jar and war both are essentially a zip archive, it gives you flexibility to extract and modify them just like any other zip file.
On windows, I think softwares like 7zip let you update the jar inline. I have done it multiple times, especially when I wanted to change application.properties alone on cloud machines, and no other code changes were required. In such cases, building the whole jar and transferring it again to cloud machine could be time consuming. So I would just extract the contents, update whatever I want to, and rezip the package.
Here is the commands you can use -
jar xf jar-file
This should extract the files into a directory.
This SO thread will guide you towards creating jar files.
Something like jar cf myJar.jar ** should be enough to generate a jar file IMO, but syntax might vary.
The jar file is actually just a zip file containing all the files and classes of your application, so technically you can change files in it like any other zip archive. Best practice is to build the jar file using Maven or Gradle from source every time you need something changed.
It's good practice to keep the source in version control using Git, and tag each build in the git repository - that way you can easily keep track of changes to the jar file by looking at what's in git at the time of the build.

launch4j and external files on classpath

I am working on a java application that uses gradle with the launch4j plugin to create a windows exe wrapper. I am required to write a 'help' framework to display help documentation to the user from within the application, and I am unsure of a method to do this, given that the only items in the classpath are the various jar files in the lib folder.
I had planned on making HTML documents for the help files, and loading these documents in the GUI through the JavaFX WebView control. With this in mind, I have the following directory structure in my application distribution:
root
|
+ config
+ help
+ lib
- Application.exe
- LICENSE
Assuming I can continue to do this as I had planned, how might I get the help and config folders into the classpath for launch4j, using only gradle config scripts (not manually editing the launch4j.xml file). If this can't be done, is there another recommended way I can include external files on the classpath so I can safely reference them using ClassLoader.getSystemResource()?
You should put your help (and config) directories as resources into the JAR. To do so with Gradle, move these directories to the src/main/resources directory. From there you can access their contents via getClass().getResource() / getClass().getResourceAsStream().

Java reloading text file from resources within jar

I have a standart maven project:
src/
| --- main/*.java
| --- resources/
|--- settings1.ini
|--- settings2.ini
That's the way I am reading this settings files:
InputStream settingsFileInputStream = ClassLoader.getSystemResourceAsStream(this.configurationFileName);
All works fine but I have to provide functionality to reload these settings1.ini and settings2.ini files at runtime when I am running maven-compiled .jar file. How can I do this?
Because as far as I know I can't access any data in .jar archive and modify it.
You can access a file in one of your application's JAR files (assuming it is on the classpath) using a stream opened using ClassLoader.getResourceAsStream(resourcePath).
However, you cannot update a file in a JAR file. Or to be more accurate:
updating a JAR (using the Java SE libraries) entails rewriting it,
there are many situations where a application won't be able to write to its JAR file,
even if it can do it, the application may only see the results of the updates after it has been restarted, and
it is a bad idea for an application to update itself in this way for various reasons ... including security.
If you want the file to be updateable, I suggest the following approach:
Pick a standard location for the file on the user's machine; e.g. on Linux, you might pick a hidden subdirectory of the current user's home directory.
On starting the application, see if the file exists, and if it doesn't populate it from the copy in the JAR file.
When the application then needs to read or update the file, read or update it at the above location.

Java resources - Use file far off in directory tree

I have a newbie Java question.
I had to make suite of J/DBUnit tests for some stored procedures we use in SQL Server. These tests use some XML files in a couple of sub-directories that I originally had placed in the same directory as my Java project.
Anyway, upon checking these tests in, our SVN manager wanted to keep the .java files in one part of the tree, and resources (like the XML files and required JARs) in another part of the tree.
So, my tests had originally referenced the XML files with a relative path which doesn't work now.
My question is:
Can I make the directories with my XML files available with the CLASSPATH (I hope so).
Assuming that works, how do I reference a file in my code that was included this way?
If I shouldn't be using the CLASSPATH for this, I'm open to other solutions.
Forget calsspath. Provide your tests with a parameter/configuration which defines the root dir for the relative paths of the XML files.
Using the classpath is no problem, the standard maven project layout looks like the following:
src
main
java
resources
test
java
resources
target
classes
test-classes
The compiler compiles src/main/java to target/classes, the resources of src/main/resources are copied to the target/classes folder, similar for the tests. If the tests have a classpath containing classes and test-classes, all works fine.
How is your project layout is, how is it build?
No, you should not use CLASSPATH in this instance since it is used by Java. However, you can use a similar approach by loading a value from an environment variable or configuration file which indicates the directory where the XML files are stored.
You can do this without making any changes to your classpath. The idea is to store the resource files in a separate directory, but have them copied to a directory in your classpath when you run your build process.
Here is an example configuration:
source Directory is ${basedir}/src/main/java
resource directory is ${basedir}/src/main/resources
In your build script, copy both the .java files and the resource files (.xml) to a directory in your classpath, say:
${basedir}/target/classes
Your test code runs against the target dir. The target directory is not checked in to SVN, keeping your SVN admin happy, and you don't have to make changes to your code.

Dependency issues in deploying using Java Webstart

I am working on a project in Java which has a directory structure something like this:
MainFolder
/ | \
Folder1 Folder2 Folder3...
|
Program.jar|Run.sh
In Folder1 I have main jar file along with the shell script to run the program.
In Folder2 I'm having configuration files in xml which may later be modified by the program
and In Folder3 I'm having jar files that the main program depends on.
Now I want to deploy this program using Java web-start.My current understanding is that web start allows us to deploy programs using 1 or more jar files.My problem is that I need the directory structure also.Can anyone suggest a solution for this.
As mentioned by others, the shell script raises problems. What does it do specifically to 'run the program'?
For the configuration files - 'Folder 2', webstart provides the PersistenceService. I have a small demo. (1)
As far as the Jars in 'Folder 3' go. Move them, as well as the Jar's in folders 2 & 1 to a single directory named 'lib'. The main Jar and the configuration files will be required eagerly, which is the default for JWS. If any of the other Jars (ex. '3') might not be needed immediately or at all, they should be deployed as download='lazy'.
To access the local file system - for reading input supplied by the user or writing a new file they created - a JWS app. normally needs to be digitally signed and trusted. But JWS also provides the much more limited form of access through the FileContents object. For more details, see the demo. of the File Service in the page linked below. (1)
1) Demos of the JNLP API, including the PersistenceService & FileContents object.
I think you will have to make some changes to the structure for webstart deployment (possibly package it as a jar or set of jars), also the launching will be done via JNLP, by webstart and not Run.sh.
Additionally you will have to sign your deployment if you need permissions to write to the disk.
See the FAQ for webstart here
Also check out the developer guide on how to deploy using webstart
Java WebStart does not provide any help in making the filsystem as you need it - it only provides the program components for memory.
If you need this structure, you will need to maintain it yourself (and then, where?)
If the only thing you need is a shell script to run, you can keep the contents of that file as a resource inside your jar, create a temporary file at each run, put the desired contents inside, and execute it, and then delete the temporary file when you are done. Note that this brings you into the wonderful world of code signing which is rather tedious.

Categories

Resources