1 | /*
|
---|
2 | Launch4j (http://launch4j.sourceforge.net/)
|
---|
3 | Cross-platform Java application wrapper for creating Windows native executables.
|
---|
4 |
|
---|
5 | Copyright (C) 2004, 2006 Grzegorz Kowal
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 2 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program; if not, write to the Free Software
|
---|
19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Created on 2005-04-24
|
---|
24 | */
|
---|
25 | package net.sf.launch4j;
|
---|
26 |
|
---|
27 | import java.io.BufferedReader;
|
---|
28 | import java.io.File;
|
---|
29 | import java.io.IOException;
|
---|
30 | import java.io.InputStream;
|
---|
31 | import java.io.InputStreamReader;
|
---|
32 | import java.io.OutputStream;
|
---|
33 | import java.io.Reader;
|
---|
34 | import java.io.Writer;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @author Copyright (C) 2005 Grzegorz Kowal
|
---|
38 | */
|
---|
39 | public class Util {
|
---|
40 | public static final boolean WINDOWS_OS = System.getProperty("os.name")
|
---|
41 | .toLowerCase().startsWith("windows");
|
---|
42 |
|
---|
43 | private Util() {}
|
---|
44 |
|
---|
45 | public static File createTempFile(String suffix) throws IOException {
|
---|
46 | String tmpdir = System.getProperty("launch4j.tmpdir");
|
---|
47 | if (tmpdir != null) {
|
---|
48 | if (tmpdir.indexOf(' ') != -1) {
|
---|
49 | throw new IOException(Messages.getString("Util.tmpdir"));
|
---|
50 | }
|
---|
51 | return File.createTempFile("launch4j", suffix, new File(tmpdir));
|
---|
52 | } else {
|
---|
53 | return File.createTempFile("launch4j", suffix);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Returns the base directory of a jar file or null if the class is a standalone file.
|
---|
59 | * @return System specific path
|
---|
60 | *
|
---|
61 | * Based on a patch submitted by Josh Elsasser
|
---|
62 | */
|
---|
63 | public static File getJarBasedir() {
|
---|
64 | String url = Util.class.getClassLoader()
|
---|
65 | .getResource(Util.class.getName().replace('.', '/') + ".class")
|
---|
66 | .getFile()
|
---|
67 | .replaceAll("%20", " ");
|
---|
68 | if (url.startsWith("file:")) {
|
---|
69 | String jar = url.substring(5, url.lastIndexOf('!'));
|
---|
70 | int x = jar.lastIndexOf('/');
|
---|
71 | if (x == -1) {
|
---|
72 | x = jar.lastIndexOf('\\');
|
---|
73 | }
|
---|
74 | String basedir = jar.substring(0, x + 1);
|
---|
75 | return new File(basedir);
|
---|
76 | } else {
|
---|
77 | return new File(".");
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static File getAbsoluteFile(File basepath, File f) {
|
---|
82 | return f.isAbsolute() ? f : new File(basepath, f.getPath());
|
---|
83 | }
|
---|
84 |
|
---|
85 | public static String getExtension(File f) {
|
---|
86 | String name = f.getName();
|
---|
87 | int x = name.lastIndexOf('.');
|
---|
88 | if (x != -1) {
|
---|
89 | return name.substring(x);
|
---|
90 | } else {
|
---|
91 | return "";
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static void exec(String cmd, Log log) throws ExecException {
|
---|
96 | BufferedReader is = null;
|
---|
97 | try {
|
---|
98 | if (WINDOWS_OS) {
|
---|
99 | cmd = cmd.replaceAll("/", "\\\\");
|
---|
100 | }
|
---|
101 | Process p = Runtime.getRuntime().exec(cmd);
|
---|
102 | is = new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
---|
103 | String line;
|
---|
104 | while ((line = is.readLine()) != null) {
|
---|
105 | log.append(line);
|
---|
106 | }
|
---|
107 | is.close();
|
---|
108 | p.waitFor();
|
---|
109 | if (p.exitValue() != 0) {
|
---|
110 | String msg = Messages.getString("Util.exec.failed")
|
---|
111 | + "(" + p.exitValue() + "): " + cmd;
|
---|
112 | log.append(msg);
|
---|
113 | throw new ExecException(msg);
|
---|
114 | }
|
---|
115 | } catch (IOException e) {
|
---|
116 | close(is);
|
---|
117 | throw new ExecException(e);
|
---|
118 | } catch (InterruptedException e) {
|
---|
119 | close(is);
|
---|
120 | throw new ExecException(e);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static void close(final InputStream o) {
|
---|
125 | if (o != null) {
|
---|
126 | try {
|
---|
127 | o.close();
|
---|
128 | } catch (IOException e) {
|
---|
129 | System.err.println(e); // XXX log
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public static void close(final OutputStream o) {
|
---|
135 | if (o != null) {
|
---|
136 | try {
|
---|
137 | o.close();
|
---|
138 | } catch (IOException e) {
|
---|
139 | System.err.println(e); // XXX log
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public static void close(final Reader o) {
|
---|
145 | if (o != null) {
|
---|
146 | try {
|
---|
147 | o.close();
|
---|
148 | } catch (IOException e) {
|
---|
149 | System.err.println(e); // XXX log
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public static void close(final Writer o) {
|
---|
155 | if (o != null) {
|
---|
156 | try {
|
---|
157 | o.close();
|
---|
158 | } catch (IOException e) {
|
---|
159 | System.err.println(e); // XXX log
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static boolean delete(File f) {
|
---|
165 | return (f != null) ? f.delete() : false;
|
---|
166 | }
|
---|
167 | }
|
---|