• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

creating .jar file at run time

i have written the code to create .jar file.
 try
        {            
            Runtime rt = Runtime.getRuntime();
          Process proc = rt.exec("jar cvf /var/apache2/htdocs/20070727.jar /usr/local/apache-tomcat/webapps/music/");
            // any error message?
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");            
             // any output?
            StreamGobbler outputGobbler = new  StreamGobbler(proc.getInputStream(), "OUTPUT");
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);  
        }
 catch (Exception t)
          {
           System.out.println(t.getMessage());
          }


Its  creating the 20070727.jar file successfully but it creates the directory structure as usr->local->apache-tomcat->webapps->music  where as my requirement is just from music directory at the top. What should i do to achieve this.

thanks in Advance.

 
0
alphamn
Asked:
alphamn
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Bart CremersJava ArchitectCommented:
You should run the jar command from a different working directory. So use:

rt.exec("jar cvf /var/apache2/htdocs/20070727.jar music/", null, new File("/usr/local/apache-tomcat/webapps/");

Although this should work, did you take a look at JarOutputStream?
0
 
CEHJCommented:
Try

Process proc = rt.exec("jar cvf /var/apache2/htdocs/20070727.jar -C /usr/local/apache-tomcat/webapps/music *");
0
 
alphamnAuthor Commented:
Bart_Cr:
the jar JarOutputStream gives me
INFO: Reloading this Context has started
OUTPUT>added manifest
OUTPUT>adding: /usr/local/apache-tomcat/webapps/music/(in = 0) (out= 0)(stored 0%)
OUTPUT>adding: /usr/local/apache-tomcat/webapps/music/index.jsp(in = 1203) (out= 612)(deflated 49%)
::
ed 60%)
 0
It success fully writes. Ur solution is still making giving me same output as my code was giving

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alphamnAuthor Commented:
CEHJ:
Sorry  no success still i get the same directory structure
0
 
CEHJCommented:
>>Sorry  no success still i get the same directory structure

I'm using exactly the same command with Runtime.exec and it works fine for me. Try

Process proc = rt.exec("jar cvf /var/apache2/htdocs/20070727.jar -C /usr/local/apache-tomcat/webapps/music .");

instead

0
 
alphamnAuthor Commented:
CEHJ:
>>Sorry  no success still i get the same directory structure

I'm using exactly the same command with Runtime.exec and it works fine for me. Try

Process proc = rt.exec("jar cvf /var/apache2/htdocs/20070727.jar -C /usr/local/apache-tomcat/webapps/music .");

instead

>> >>>>>>>>>>>>>>>>>>>>
Yes its working fine tell me if  i use the same on a windows environment  like
Process proc = rt.exec("jar cvf E:/20070727.jar -C F:/music .");
Will it give me same  output ? Right now in my case when i run on a windows environment the same command apart from the directory structure it make all the java shell command as part of the  jar file
0
 
CEHJCommented:
>>it make all the java shell command as part of the  jar file
      
In what way? A jar file can only contain entries. Do you mean the file *name*? If so, what is it?
0
 
alphamnAuthor Commented:
CEH J:

On running the same command
Process proc = rt.exec("jar cvf E:/20070727.jar -C F:/music .");


The entries that i get in jar file are  like
META-INF
F:
tomcat-native.tar.gz
tomcat.util.gz
jmx.jar
:
:
:
catalina.bat
catalina-tasks.xml
0
 
CEHJCommented:
Yes, you'd probably be better not running that across drives like that
0
 
objectsCommented:
use a batch to copy the files and create your jar, then run that from exec()
or better still use anty to create the jar, and run ant using exec()
0
 
Bart CremersJava ArchitectCommented:
it might be necessary to use backslashes instead of forward slashes when running on windows.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now