Solution Advice: Webapp

Encrypt and wrap a webapp (war) into a single exe file


Introduction

Webapp is a group of files (jsp, htm, java, xml etc) to perform a web site under a web server such as Tomcat etc.

If you want to encrypt and protect your webapp, and wrap it into a single exe file, and run it as a background service, this advice will show you how to do that, wrap and encrypt a webapp into a single exe file step by step.

 There is another easier way for reference : http://github.com/sswater/webapp-solution


Solution

We select 'tomcat-embed' as the web server. In our starter program, we create a Tomcat instance and call its start() to start the web server.

We will let the programs, such as jsp, jspx, jar and java files etc be 'encrypted' by Jar2Exe. The jsp files cannot be compiled at runtime because required classes are encrypted, so we must pre-compile the webapp before we can wrap into exe.

Because we want to get only ONE single exe file as result, we need to do little modification on tomcat that we need to let tomcat regard ".exe" file as a ".war" file. Of course, we do not really "modify" source code of tomcat, but inherit and overwrite one or two methods, by just modifying one or two lines.


Features used

Following features or third-party libraries are selected in this solution:

Feature Advice Introduction How to enable

Tomcat embed

Required

We select tomcat embed as the web server.

Download from local site, or pom.xml

Encryption

Required

We will let the programs be encrypted, while we let other plain resources such as htm, gif, xml etc keep accessible.

At Step 4, check the encrypt option

Service application

Optional

We discuss how to protect and wrap webapp into one exe in this chapter. You can select to run as either a console program or a service program.

At Step 2, select Service application

Depend jar files

Required

We wrap all tomcat jar files and jars of webapp into a single exe file.

At Step 5, add jar files

Stdout redirect

Recommended

Tomcat will write logs to System.out, so it is recommended to redirect output to disk file as a log file.

At Step 6 > Config Internal > Custom tab, use a 'stdout' item

Stdout redirect
to cronolog

Optional

You can redirect stdout to a pipe, "| cronolog.exe %Y%m%d.log", to split output to daily log files.

Use a "stdout | cronolog.exe" in config

Force CWD

Recommended

Disk IO, such as write log files with 'relative path' are based from the "working directory".

At Step 6 > Config Internal > Custom tab, use a 'forcecwd' item

Program icon

Optional

You can decide whether to select an icon, not necessary.

At Step 6 > Version & messages


Steps with example

We select the 'Tomcat/webapps/examples' as our demo webapp. The steps are a bit more complex than a common java program, so please prepare a working directory, for example "C:\tempspace\" or any directory else. Now let's begin:

  1. We need JDK and ANT installed before we can go on with this demo.
  2. Download tomcat-embed.zip from local site and extract to C:\tempspace\ , then we get:
    C:\tempspace\
    │  build.xml
    │
    └─lib\
  3. Copy the 'examples' webapp from Tomcat/webapps/examples to C:\tempspace\. If you have a war file, please extract it. Then we get:
    C:\tempspace\
    │  build.xml
    │
    ├─examples\
    │  └─WEB-INF\
    │      ├─classes\
    │      └─lib\
    └─lib\
  4. Pre-compile the webapp at command line by ant tool:
    C:\tempspace> ant -Dtomcat.home=. -Dwebapp.path=examples
  5. By the instructions of pre-compile webapp, we insert the contents of C:\tempspace\examples\WEB-INF\generated_web.xml into beginning of C:\tempspace\examples\WEB-INF\web.xml:
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">
    
        <!-- INSERT contents of generated_web.xml HERE -->
    
        <description>
          Servlet and JSP Examples.
        </description>
        <display-name>Servlet and JSP Examples</display-name>
    
    ......
    
  6. Move all jar files out of webapp, that is to move from C:\tempspace\examples\WEB-INF\lib to C:\tempspace\lib
  7. Delete all programs in webapp, that is to delete all *.jsp, *.jspx, *.java(excluding static java that just for download), *.class(excluding Applet for browser client use) files in 'examples', and delete directory C:\tempspace\examples\WEB-INF\classes and C:\tempspace\examples\WEB-INF\lib .
  8. Wrap left contents of webapp into a zip file, that is to compress all files in C:\tempspace\examples\* into C:\tempspace\examples.zip:
    C:\tempspace>jar cvf examples.zip -C examples/ .
  9. Write the starter program, using the modified Tomcat: ( Source code and modified tomcat)
    package hello;
    
    import org.apache.catalina.Context;
    import org.apache.catalina.core.StandardContext;
    import org.apache.catalina.startup.Tomcat;
    
    import com.regexlab.j2e.tomcat_modified.Jar2ExeTomcat;
    
    /**
     * demo starter for Jar2Exe
     * 
     * @author sswater
     */
    public class HelloWebapp {
    
        public static void main(String[] args) throws Exception {
            
            // Tomcat instance
            Tomcat tomcat = new Jar2ExeTomcat();
            tomcat.setPort(80);
            tomcat.setBaseDir(System.getProperty("java.io.tmpdir", "."));
    
            // deploy exe file self as a war file
            Context context = tomcat.addWebapp("/", System.getProperty("j2e.app.path"));
            
            // do not extract war file
            ((StandardContext)context).setUnpackWAR(false);
            
            // Start server
            tomcat.start();
            tomcat.getServer().await();
            
        }
    }
    
  10. Compile the starter program and wrap into a jar file, C:\tempspace\hellowebap.jar . ( Download the starter) Now we should get following files:
    C:\tempspace\
    │  build.xml
    │  examples.zip
    │  hellowebapp.jar
    │
    ├─examples\
    └─lib\
            ecj-3.7.jar
            jstl.jar
            standard.jar
            tomcat-api-7.0.8.jar
            tomcat-el-api-7.0.8.jar
            tomcat-embed-core-7.0.8.jar
            tomcat-jasper-7.0.8.jar
            tomcat-jasper-el-7.0.8.jar
            tomcat-jsp-api-7.0.8.jar
            tomcat-juli-7.0.8.jar
            tomcat-servlet-api-7.0.8.jar
            tomcat-util-7.0.8.jar
            WEB-INF-classes.jar
    
  11. Start Jar2Exe, at step 1, click Browse Jar and select 'C:\tempspace\hellowebapp.jar'
  12. At step 2, select Console application, or one of other type if you like.
  13. At step 3, select the main class hello.HelloWebapp
  14. At step 4, check the Encrypt and hide class files option.
  15. At step 4, at the 'These files keep not hidden' box, input C:\tempspace\examples.zip
  16. At step 5, click Add, browse to C:\tempspace\lib and add all jar files in the directory.
  17. At step 5, select one or more or all jar files in the list box, and click Protect to encrypt selected jar files.
  18. At step 6, click "Version & Message", then
    1. At "Product Info" tab, click Add to add a program icon.
    2. Click OK to close this popup dialog.
  19. At step 6, click "Config Internal", or at step 7(Finished), click "Config External", then
    1. At Custom tab, append "forcecwd ." to set the "working directory".
    2. At Custom tab, append "stdout out.log" to redirect System.out to a disk file.
    3. Click OK to close this popup dialog.
  20. Click Next until finish.

Download

Comments

Add new comment