Pack a jar to work as a blackbox (with resources) - java

I have a rather large project, which uses a lot of jars, all meant for normal usage and therefore use files as the following:
new File(relativePath);
Now I need to use that project as part of a Jersey/Maven/Glassfish project, which surely enough has a much better way to load file, namely as an InputStream.
Now, some of the files in my original project are only compiled classes which means I can't change the way they load the files (I can change the file's path though).
I've packed the entire project using the 'pack for store' and gave it a try, but all I got was a FileNotFoundException.
Is there any way to pack the project along with all the resources to run as a black box, or as if it was running locally?
Any other ideas on how to do that would also be greatly appreciated.

Related

How do I access Java resources in a Grails project?

Grails newbie here. My application is in Grails, my IDE is IntelliJ IDEA. I configured my project (in IntelliJ) to say that my resources folder is under root\src\resources. I have an image file in there that I need to load into an InputStream / BufferedImage. However I can't seem to get to those resources from my Grails controller. This is what I was trying:
def image = this.getClass().getClassLoader().getResourceAsStream("image.png")
But that just returns null. I use this exact same convention in Java projects (well, except I have to declare image being of type InputStream), and it does work there. I was under the impression that I could essentially drop Java code in a Grails project and it should just work. What do I need to do differently to get this to work in my Grails controller? I need to access that static resource file.
If you mark a directory as a source directory in IntelliJ IDEA, Grails won't know about it. You have to configure Grails properly by either adding your new directory as a source directory or move the resource to one of the standard source directories.
I've never actually added a new source directory myself, but the answer to this stackoverflow question looks promising.
Other than that, you can just add resources to any source directory and it will be included, for example: grails-app/conf, src/java, src/groovy and more. In addition, any file in web-app/META-INF/classes will also be in the classpath of the application. The last one is great to know about if you need to copy a java or groovy source file (i.e. just copy, no compilation).
Try this
servletContext.getResourceAsStream("/WEB-INF/resources/yourfile")

Packaging external files with JavaFX deployment

I have a JavaFX app that reads in a configuration file. I'd like the config file to remain outside of the jar to facilitate modification without recompiling. Is there a way to set up netbeans to grab the config file and include it in the installer?
I've found the option to change to icon and that works fine but I haven't been able to discover how to tell it to also include specific external resources.
I've read the information posted here: http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm but I'm still not seeing a way to accomplish this.
I don't think there's a way to do this (though I may be wrong). I needed something similar to this once, and the approach I took was
Package the file in the jar file
At program start-up time, check to see if the file exists in the expected location on the local drive
If it's there, read it, etc
Otherwise, read the contents from the jar and write them to the expected file
This solves the problem of "deploying outside the jar", and it also solves the problem of the user inadvertently deleting the file after deployment, etc.
The way I solved this issue (due to having many dependent .dll's and other type items) was to use netbeans to compile the jar, which has things like images/css/fxml etc. etc. and then use an Inno Script to actually compile and configure the installer, since inno makes it pretty straightforward to include extra resources. I have yet to find a way to do this properly within javafx itself.

What is the best practice to recover from a .war file into a web application

Our organization invested in a java web application that we want to be able to develop upon. Unfortunately last developer left with only the .war file. How can we recover some of the class files, change them then redeploy as war again. We tried to do that in Eclipse but after importing as war, we could not configure build path. (which in my understanding, the war was imported as static project). I could not find "Convert to dynamic project" as said in eclipse help document.
A .jar/.war file is compressed using the ZIP format. You can decompress it using any zip utility like WinZIP/WinRAR/etc. and then use your favorite decompiler on the .class files.
Java Decompiler is the best I've ever come across. You could open your entire war with this.
There is another option for special cases: Have a look at aspectj, so you can
weave in additional code around method calls to fix bugs etc.
add fields or methods etc.
It works on the byte code level (it works with .class files).
As for the long run, grab what you recover using a decompiler, and start to replace class files step by step.

How can I export my program in an executable format (Java, eclipse)?

I have created a program in Java using eclipse that contains a couple of folders with graphics and files that get read from and written to. What I need is a way to export the whole program in some executable format so that anyone can run my program.
I've had a look around online and I notice that people suggest creating an executable JAR file. However I have my reservations about this since I suspect it will choose to ignore the graphics & other files that the program uses, only focusing on the actual source code.
Please could someone suggest a solution to this issue, it is absolutely essential that the files and graphics are packaged up with the rest of the code.
On another related note; at present I'm referencing the files & graphics using files paths that are specific to my computer. If I were to use another solution such as creating an installable program how should I handle these filepaths? Apologies if this is a naive question, however I'm new to this sort of thing.
However I have my reservations about this since I suspect it will choose to ignore the graphics & other files that the program uses, only focusing on the actual source code.
When you think you may have a solution but it doesn't work, you should test that theory.
A jar file is absolutely the right solution for this. However, you need to make sure that Eclipse considers them as resources on the build path so that it will copy them into the jar file. Then you just need to refer to them from the jar file:
On another related note; at present I'm referencing the files & graphics using files paths that are specific to my computer. If I were to use another solution such as creating an installable program how should I handle these filepaths?
Use Class.getResource() or Class.getResourceAsStream() or the ClassLoader equivalents. That will let you load your resources directly from the jar file, without even having separate files on the file system.

Problems importing WAR files in Eclipse?

I was unfortunately forced to result to uploading a WAR file as my backup for a web application I am working on.
Luckily I have the most recent WAR file available. I am using Eclipse IDE and am using the Web Tools plugin for all the J2EE work that I am doing with the Dynamic Web Application Project.
When I imported my WAR file, and ran it on a local server, everything works fine. The problem I a ran into is that in the Java Resources/src folder that all my packages and .java files were now only consists of all the same packages, but they are empty.
I checked to see if I could find the files and I found the .class files in an "Imported files" folder that is not accessible in the Eclipse Project Explorer. I believe that I need to do some type of build or something so that my .java files are available for me, but unfortunately this is one area where I lack.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
Also, I would like to configure this environment as it was before where my Java Resources:src folder contaiend the packages and .java files.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
The short answer is No. There is no way to regenerate original source files from bytecode files.
If you were really, really desperate you could try to use a Java bytecode decompiler on your bytecode files, but the result will be be nothing like your original source code.
All comments and javadocs will be gone.
All original code layout will be gone.
Original local variable and parameter names may be gone, depending on your original compiler switches.
Constant expressions may have been pre-evaluated, and loops, string concatenations and other constructs may have been transformed unrecognizably.
Depending on the maturity of the decompiler, the Java code might not be semantically equivalent to the original code, and might not even be compilable.
I hope you haven't spent too long developing this application because the best answer may be to start again.

Categories