2 Launch4j (http://launch4j.sourceforge.net/)
3 Cross-platform Java application wrapper for creating Windows native executables.
5 Copyright (C) 2004, 2006 Grzegorz Kowal
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.
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.
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
23 * Created on 2005-04-24
25 package net.sf.launch4j;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
32 import net.sf.launch4j.binding.InvariantViolationException;
33 import net.sf.launch4j.config.Config;
34 import net.sf.launch4j.config.ConfigPersister;
37 * @author Copyright (C) 2005 Grzegorz Kowal
39 public class Builder {
40 private final Log _log;
42 public Builder(Log log) {
47 * @return Output file path.
49 public File build() throws BuilderException {
50 final Config c = ConfigPersister.getInstance().getConfig();
53 } catch (InvariantViolationException e) {
54 throw new BuilderException(e.getMessage());
59 FileInputStream is = null;
60 FileOutputStream os = null;
61 final RcBuilder rcb = new RcBuilder();
63 File basedir = Util.getJarBasedir();
65 ro = Util.createTempFile("o"); //$NON-NLS-1$
66 outfile = ConfigPersister.getInstance().getOutputFile();
68 Cmd resCmd = new Cmd(basedir);
69 resCmd.addExe("windres") //$NON-NLS-1$
70 .add(Util.WINDOWS_OS ? "--preprocessor=type" : "--preprocessor=cat") //$NON-NLS-1$ //$NON-NLS-2$
71 .add("-J rc -O coff -F pe-i386") //$NON-NLS-1$
74 _log.append(Messages.getString("Builder.compiling.resources")); //$NON-NLS-1$
77 Cmd ldCmd = new Cmd(basedir);
78 ldCmd.addExe("ld") //$NON-NLS-1$
79 .add("-mi386pe") //$NON-NLS-1$
80 .add("--oformat pei-i386") //$NON-NLS-1$
81 .add((c.getHeaderType() == Config.GUI_HEADER)
82 ? "--subsystem windows" : "--subsystem console") //$NON-NLS-1$ //$NON-NLS-2$
83 .add("-s") // strip symbols //$NON-NLS-1$
84 .addFiles(c.getHeaderObjects())
86 .addFiles(c.getLibs())
87 .add("-o") //$NON-NLS-1$
89 _log.append(Messages.getString("Builder.linking")); //$NON-NLS-1$
92 if (!c.isDontWrapJar()) {
93 _log.append(Messages.getString("Builder.wrapping")); //$NON-NLS-1$
95 byte[] buffer = new byte[1024];
96 is = new FileInputStream(
97 Util.getAbsoluteFile(ConfigPersister.getInstance().getConfigPath(), c.getJar()));
98 os = new FileOutputStream(outfile, true);
99 while ((len = is.read(buffer)) > 0) {
100 os.write(buffer, 0, len);
103 _log.append(Messages.getString("Builder.success") + outfile.getPath()); //$NON-NLS-1$
105 } catch (IOException e) {
106 Util.delete(outfile);
107 _log.append(e.getMessage());
108 throw new BuilderException(e);
109 } catch (ExecException e) {
110 Util.delete(outfile);
111 String msg = e.getMessage();
112 if (msg != null && msg.indexOf("windres") != -1) { //$NON-NLS-1$
113 _log.append(Messages.getString("Builder.generated.resource.file")); //$NON-NLS-1$
114 _log.append(rcb.getContent());
116 throw new BuilderException(e);
127 private final StringBuffer _sb = new StringBuffer();
128 private final File _basedir;
129 private final File _bindir;
130 private final boolean _quote;
132 public Cmd(File basedir) {
134 _quote = basedir.getPath().indexOf(' ') != -1;
135 String path = System.getProperty("launch4j.bindir");
137 _bindir = new File(basedir, "bin");
139 File bindir = new File(path);
140 _bindir = bindir.isAbsolute() ? bindir : new File(basedir, path);
144 public Cmd add(String s) {
150 public Cmd addAbsFile(File file) {
152 boolean quote = file.getPath().indexOf(' ') != -1;
156 _sb.append(file.getPath());
163 public Cmd addFile(String pathname) {
168 _sb.append(new File(_basedir, pathname).getPath());
175 public Cmd addExe(String pathname) {
180 if (Util.WINDOWS_OS) {
181 pathname += ".exe"; //$NON-NLS-1$
183 _sb.append(new File(_bindir, pathname).getPath());
190 public Cmd addFiles(String[] files) {
191 for (int i = 0; i < files.length; i++) {
197 private void space() {
198 if (_sb.length() > 0) {
203 public String toString() {
204 return _sb.toString();
207 public void exec(Log log) throws ExecException {
208 Util.exec(_sb.toString(), log);