Home > Software design >  Java Tomcat: Continuing development with deployed WAR file (without source code on hand)
Java Tomcat: Continuing development with deployed WAR file (without source code on hand)

Time:12-18

I have a compiled war file that I downloaded from remote server. I could run the application locally. The problem is that I don't have a source code and all java files are already compiled .class files.

Now I want to add some new functionality to the existing project. The application is old and it uses web.xml file to declare servlets. E.g:

    <servlet>
        <servlet-name>ServletOne</servlet-name>
        <servlet-class>path.to.ServletOne</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ServletTwo</servlet-name>
        <servlet-class>path.to.ServletTwo</servlet-class>
    </servlet>
...

I tried to add a new servlet path in web.xml file like:

    <servlet>
        <servlet-name>ServletOne</servlet-name>
        <servlet-class>path.to.ServletOne</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ServletTwo</servlet-name>
        <servlet-class>path.to.ServletTwo</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>path.to.MyServlet</servlet-class>
    </servlet>
...

Then I added a new .java file under path.to.MyServlet and copy- pasted all the code from another (compiled) servlet into my new servlet class (I can see the content of .class files, because there is such a functionality in the IDE I'm using, but those classes are in read-only mode. However I cannot compile MyServlet file (cannot convert MyServlet.java to MyServlet.class. Because the rest of the classes are already compiled (.class format, not .java) and all the import codes are not working (MyServlet cannot see other files).

Here is one of the errors I'm getting when I try javac MyServlet.java:

error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;

Of course I can use something like javac -cp path/to/tomcat/servlet-api.jar MyServlet.java ,but what If I will need to import classes that are already in the project? It will get kinda messy, I suppose...

What is the most organic way of continuing development without having the source code?

What I was thinking is building a separate Spring Boot application, however it will be in a different port and there is a requirement of having one time authentification (Meaning, I cannot ask a user to login again when he/she switches between the applications).

I'm using java-8 and the application is deployed on tomcat-7 (If it's helpful)

CodePudding user response:

If you’re planning to make more changes ongoing, and if decompiling the classes is really permissible in your situation, I think the simplest solution is to create a new project, decompile all of the existing classes, and add them to the new project.

If it’s a one-time change, you can unzip the war file, add the unzipped WEB-INF/classes directory to the classpath for the new file you are compiling (for example, javac -cp path/to/tomcat/servlet-api.jar:path/to/unzipped-warfile/WEB-INF/classes MyServlet.java), then add the new compiled class to WEB-INF/classes and repackage into a war file.

  • Related