KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > JarUtil


1 /*
2  
3    Derby - Class org.apache.derbyTesting.functionTests.util.JartUtil
4  
5    Licensed to the Apache Software Foundation (ASF) under one
6    or more contributor license agreements. See the NOTICE file
7    distributed with this work for additional information
8    regarding copyright ownership. The ASF licenses this file
9    to you under the Apache License, Version 2.0 (the
10    "License"); you may not use this file except in compliance
11    with the License. You may obtain a copy of the License at
12  
13      http://www.apache.org/licenses/LICENSE-2.0
14  
15    Unless required by applicable law or agreed to in writing,
16    software distributed under the License is distributed on an
17    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18    KIND, either express or implied. See the License for the
19    specific language governing permissions and limitations
20    under the License.
21  */

22
23 package org.apache.derbyTesting.functionTests.util;
24 import java.io.*;
25
26
27 /**
28  *
29  * <Put Class Comments Here>
30  */

31 public class JarUtil {
32
33     /**
34      * Unjar a file into the specified directory. This runs in a separate
35      * process. Note, your test needs security permissions to read user.dir
36      * and to start a process for this to work.
37      *
38      * @param jarpath - Path to jar file
39      *
40      * @param outputdir - The directory to unjar to. If this is null,
41      * we user user.dir (the current directory)
42      *
43      */

44     public static void unjar(String JavaDoc jarpath, String JavaDoc outputdir)
45         throws ClassNotFoundException JavaDoc, IOException, InterruptedException JavaDoc
46     {
47         if ( outputdir == null ) {
48             outputdir = System.getProperty("user.dir");
49         }
50         File jarFile = new File((new File(outputdir, jarpath)).getCanonicalPath());
51
52         // Now unjar the file
53
String JavaDoc jarCmd = "jar xf " + jarFile.getPath();
54         // Now execute the jar command
55
Process JavaDoc pr = null;
56         try
57         {
58             //System.out.println("Use process to execute: " + jarCmd);
59
pr = Runtime.getRuntime().exec(jarCmd);
60
61             pr.waitFor();
62             //System.out.println("Process done.");
63
pr.destroy();
64         }
65         finally {
66             if (pr != null)
67             {
68                 pr.destroy();
69                 pr = null;
70             }
71         }
72     }
73 }
Popular Tags