Java .War File Compilation - java

I need some help figuring out how to make some changes to some .jsp pages that are contained inside of a .war package.
I am using jbilling opensource billing software and need to modify some elements/display and want to do it directly in the .jsp, so I unpacked the .war file and made a change, then packaged it back up and put it inside the webapps folder and restarted tomcat. But I noticed the filesize from the .war that I packaged was smaller than the original .war and it should have been larger because I added stuff to it. Needless to say, tomcat didn't start up properly, or at least there were errors in the logs..but they didn't help me and jbilling didn't work right. I reverted back to the orignal .war and it worked fine.
Is it possible for me to unpackage a .war and simply make some html changes, then repackage it up without having to recompile the whole source code with the java classes? Did I use the wronge "packaging" tool to compile it? Is there another way to accomplish what I'm trying to?
I did this in a FreeBSD box with using the following commands:
unpackage-->sudo jar cf ../billing.war *
repackage-->sudo jar -xvf billing.war
Thanks for your help.

Moreover, .WAR and .JAR (AFAIK all it goes for all .*AR) are basically glorified zip files conforming to some structural requirements (manifests, web app descriptors, etc.). The easiest way to do the changes you want are to handle the .WAR file as if it was a plain-simple ZIP file. The choice of the tools is all yours (archiver, pkzip, etc.).

You can simply open war file using archiver utility (seems you are using ubuntu).
Open jsp file from archiver util it self , make changes , and Archiver util will ask that jsp file has been modified you want to update your war file say yes there.
Note: If you are going to do only view changes than go for this tricky way otherwise if you are willing to change source java files than building the war will be strongly recommended

Related

How to refresh a java application after making a file change

This may sounds like a dumb question to some, but I am not a Java developer, we have moved a Java app from Rackspace server to AWS linux server and upgraded MySql from 5.2.x to 5.7.x. Some SQL statements are causing issues so I am trying to fix those inside the .java files. However, when I save the file, and use .sftp library from VSCode to push the changes to the linux server, then refresh the site, it does not reflect the changes. Is there anything else I need to be doing every time I make a change?
I was able to update some string in a .jsp file and that worked when I refreshed, so I am not sure why when I change a .java file, it does not reflect change.
The entire directory is confusing to me, I believe it was done so that all files, configs, tomcat files, etc. would be under one git folder so that it can just be dropped anywhere. I find a lot of sources state that a .war file would be created and you would place that file in the tomcat apps directory.
Here is the basic layout of the directory of the app
-www/live/njun
-build.xml
+lib
+scripts
+sql
+src
-tomcat(has some config files, scripts, webapps folder)
-web
-content
-index.jsp
-trampoline
-includes folders with .java files
-tickets
-ticketsMgr.java (this is the file I modified but I could not see any differences in app)
-index.jsp (this is one I modified and did reflect in browser)
-WEB-INF
-classes/com/njun
-lots of folder that have .Class files(although when I open them it's all gibberish characters)
This is the basic structure of what I am looking at.
I tried going to the /opt/tomcat/bin and > sh startup.sh - to restart tomcat. That didn't work.
I also ran the > sh startup.sh version inside of the tomcat folder above,
/tomcat/bin/sh startup.sh - that shows me some info where the home, base tmpdir, jre_home, etc. But that didn't work neither.
I am not being asked to make major modifications on this app, I figure I can manage the SQL errors myself. If we get to the point where we need enhancements done to this app, we would hire a Java developer.
Please let me know if I am missing something and I can provide.
Thanks in advance,
That's because a .java file is just source code. You need to compile it and upload the corresponding .class file generated when you compile the .java sources. How to compile and run a Java program. Or well, since it runs on a Tomcat instance, you might have a JAR or WAR file. But the idea is still the same, since those are just basically zip files that hold your .class files (among other stuff that's less relevant for your issue).

Properly deploying a small Java project

I'm currently working on a small Java Project (~30 Classes, 5 external libs).
The code accesses resources in the folders src/resources and src/test_resources using getClass.getResouce("/resources/any.file").
Most of these resource files will probably never be touched by a user, but there are also some regular configuration files which are intended to be edited by the end users.
My question now is: How should I be deploying such an application?
Exporting everything into a runnable jar doesn't seem to be a good way, as I don't wanna torture my users and let them unzip the jar for editing the configuration files.
Should I export all of the internal stuff into the jar, and copy the resources directory into a Folder side by side with it? How can I access the resources then?
Thank you guys!
You could copy the resources folder. It doesn't necessarily need to be side-by-side with the jar file. The key is that you need to put the parent folder of the resources folder on the classpath.
For example, you could copy it someplace like:
c:\some\directory\resources
Then, when you execute, do something like:
java -cp c:\some\directory;c:\some\path\to\your.jar;... your.main.ClassName

Adding jar files in the WEB-INF/lib folder

I am using spring MVC validation for my login form in eclipse.
Here i want to add my jar files (Hibernate-validator,javax.validator etc..) into the lib folder in the WEB CONTENT.But if every time i add it will get into Javascript Resources.
I do found these question in forum but still cant get the right answer.Can any suggest me want i have to do.
Check if any copy of it is actually physically being copied to your javascript resoures at runtime. If so there might be some setup which is doing so, otherwise there is no possibility of your jar files being copied to your javascript resources.

Files not accessible in Deployed Java class, but working locally in Web app developed on play 2.2 framework

I have a web application that I developed in Java over Play 2.2 framework.
When I am running the app locally one of my java class which is accessing some files in the project folders run fine.
After I create a jar file and run it, the java code is not able to read those files. The files are in the public folder and I have ensured packaged in the jar.
There are other files which I have referred from HTML and they are accessible but the java classes are not able to access the files.
Please help me solve this issues.
I think you are trying to access a file inside a jar. If I have understood your question. Please correct me if I am wrong
You could use something like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileFromJarFile);
If test.txt was in the root of your JAR file, you'd use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.txt");
NOTE : The file and the java class needs to be in same jar file.
The code was all fine in my case, the directory structure at the jar side was where I missed the part.
I had to keep the files/folders in the directory as same as that of the jar as #APaul said, I tried that but it did not work because I was using a batch to run the jar. My files should have been in the folder containing the batch file. My bad!
Anyways Thanks #APaul.

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.

Categories