1 | import java.io.*; |
2 | import java.lang.reflect.InvocationTargetException; |
3 | import java.lang.reflect.Method; |
4 | import java.net.MalformedURLException; |
5 | import java.net.URL; |
6 | import java.net.URLClassLoader; |
7 | import java.util.ArrayList; |
8 | import java.util.List; |
9 | |
10 | /** |
11 | * javax by Stefan Reich (www.superinformatiker.de, info@superinformatiker.de) |
12 | * |
13 | * jaxax compiles and runs a bunch of Java sources in one step. |
14 | * |
15 | * Syntax: |
16 | * javax (searches for sources in current dir) |
17 | * javax srcdir (searches in given directory) |
18 | * |
19 | * Main class must be called "main" in default package. |
20 | * |
21 | * Requirements: |
22 | * Java 6 or higher - JDK (javac) must be in path |
23 | * |
24 | * Limitations: |
25 | * Linux only, right now (invokes bash to call javac) |
26 | * Can't pass arguments to invoked program yet (would be easy to add) |
27 | * |
28 | * TODO: delete .class files at program end |
29 | */ |
30 | |
31 | public class javax {
|
32 | public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
33 | File srcDir = new File(args.length == 0 ? "." : args[0]); |
34 | List<File> sources = new ArrayList<File>(); |
35 | System.out.println("Scanning for sources in " + srcDir.getPath());
|
36 | scanForSources(srcDir, sources); |
37 | if (sources.isEmpty()) {
|
38 | System.out.println("No sources found");
|
39 | return; |
40 | } |
41 | File optionsFile = File.createTempFile("javax", "");
|
42 | File classesDir = new File(System.getProperty("user.home"), ".javax/" + System.currentTimeMillis());
|
43 | System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
|
44 | String options = "-d " + bashQuote(classesDir.getPath()); |
45 | writeOptions(sources, optionsFile, options); |
46 | classesDir.mkdirs(); |
47 | invokeJavac(optionsFile); |
48 | System.out.println("Running program (class main.java)\n");
|
49 | runProgram(classesDir); |
50 | } |
51 | |
52 | private static void runProgram(File classesDir) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
53 | URLClassLoader classLoader = new URLClassLoader(new URL[]{classesDir.toURI().toURL()});
|
54 | Class<?> mainClass = classLoader.loadClass("main");
|
55 | Method main = mainClass.getMethod("main", String[].class);
|
56 | main.invoke(null, (Object) new String[0]); |
57 | } |
58 | |
59 | private static void invokeJavac(File optionsFile) throws IOException {
|
60 | String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
|
61 | System.out.println(javacOutput); |
62 | } |
63 | |
64 | private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
|
65 | FileWriter writer = new FileWriter(sourcesFile); |
66 | for (File source : sources) |
67 | writer.write(bashQuote(source.getPath()) + " "); |
68 | writer.write(moreOptions); |
69 | writer.close(); |
70 | } |
71 | |
72 | private static void scanForSources(File source, List<File> sources) {
|
73 | if (source.isFile() && source.getName().endsWith(".java"))
|
74 | sources.add(source); |
75 | else if (source.isDirectory()) {
|
76 | File[] files = source.listFiles(); |
77 | for (File file : files) |
78 | scanForSources(file, sources); |
79 | } |
80 | } |
81 | |
82 | public static String backtick(String cmd) throws IOException {
|
83 | File outFile = File.createTempFile("_backtick", "");
|
84 | File scriptFile = File.createTempFile("_backtick", "");
|
85 | |
86 | String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; |
87 | //Log.info("[Backtick] " + command);
|
88 | try {
|
89 | saveTextFile(scriptFile.getPath(), command); |
90 | String[] command2 = {"/bin/bash", scriptFile.getPath() };
|
91 | Process process = Runtime.getRuntime().exec(command2); |
92 | try {
|
93 | process.waitFor(); |
94 | } catch (InterruptedException e) {
|
95 | throw new RuntimeException(e); |
96 | } |
97 | int value = process.exitValue(); |
98 | //Log.info("exit value: " + value);
|
99 | return loadTextFile(outFile.getPath(), ""); |
100 | } finally {
|
101 | scriptFile.delete(); |
102 | } |
103 | } |
104 | |
105 | /** possoibly improvable */ |
106 | public static String bashQuote(String text) {
|
107 | if (text == null) return null; |
108 | return "\"" + text |
109 | .replace("\\", "\\\\")
|
110 | .replace("\"", "\\\"")
|
111 | .replace("\n", "\\n")
|
112 | .replace("\r", "\\r") + "\"";
|
113 | } |
114 | |
115 | public final static String charsetForTextFiles = "UTF8"; |
116 | |
117 | /** writes safely (to temp file, then rename) */ |
118 | public static void saveTextFile(String fileName, String contents) throws IOException {
|
119 | File file = new File(fileName); |
120 | File parentFile = file.getParentFile(); |
121 | if (parentFile != null) |
122 | parentFile.mkdirs(); |
123 | String tempFileName = fileName + "_temp"; |
124 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
125 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
126 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
127 | printWriter.print(contents); |
128 | printWriter.close(); |
129 | if (file.exists() && !file.delete()) |
130 | throw new IOException("Can't delete " + fileName);
|
131 | |
132 | if (!new File(tempFileName).renameTo(file)) |
133 | throw new IOException("Can't rename " + tempFileName + " to " + fileName);
|
134 | } |
135 | |
136 | public static String loadTextFile(String fileName, String defaultContents) throws IOException {
|
137 | if (!new File(fileName).exists()) |
138 | return defaultContents; |
139 | |
140 | FileInputStream fileInputStream = new FileInputStream(fileName); |
141 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
142 | return loadTextFile(inputStreamReader); |
143 | } |
144 | |
145 | public static String loadTextFile(Reader reader) throws IOException {
|
146 | StringBuilder builder = new StringBuilder(); |
147 | try {
|
148 | BufferedReader bufferedReader = new BufferedReader(reader); |
149 | String line; |
150 | while ((line = bufferedReader.readLine()) != null) |
151 | builder.append(line).append('\n');
|
152 | } finally {
|
153 | reader.close(); |
154 | } |
155 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
156 | } |
157 | } |
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
| ID | Author/Program | Comment | Date | |
|---|---|---|---|---|
| 908 | #1000604 (pitcher) | 2015-08-20 15:28:24 | ||
| 897 | #1000610 | Edit suggestion: !636 !629 main { static Object androidContext; static String programID; public static void main(String[] args) throws Exception { import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; /** * javax by Stefan Reich (www.superinformatiker.de, info@superinformatiker.de) * * jaxax compiles and runs a bunch of Java sources in one step. * * Syntax: * javax (searches for sources in current dir) * javax srcdir (searches in given directory) * * Main class must be called "main" in default package. * * Requirements: * Java 6 or higher - JDK (javac) must be in path * * Limitations: * Linux only, right now (invokes bash to call javac) * Can't pass arguments to invoked program yet (would be easy to add) * * TODO: delete .class files at program end */ public class javax { public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { File srcDir = new File(args.length == 0 ? "." : args[0]); List<File> sources = new ArrayList<File>(); System.out.println("Scanning for sources in " + srcDir.getPath()); scanForSources(srcDir, sources); if (sources.isEmpty()) { System.out.println("No sources found"); return; } File optionsFile = File.createTempFile("javax", ""); File classesDir = new File(System.getProperty("user.home"), ".javax/" + System.currentTimeMillis()); System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath()); String options = "-d " + bashQuote(classesDir.getPath()); writeOptions(sources, optionsFile, options); classesDir.mkdirs(); invokeJavac(optionsFile); System.out.println("Running program (class main.java)\n"); runProgram(classesDir); } private static void runProgram(File classesDir) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { URLClassLoader classLoader = new URLClassLoader(new URL[]{classesDir.toURI().toURL()}); Class<?> mainClass = classLoader.loadClass("main"); Method main = mainClass.getMethod("main", String[].class); main.invoke(null, (Object) new String[0]); } private static void invokeJavac(File optionsFile) throws IOException { String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath())); System.out.println(javacOutput); } private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException { FileWriter writer = new FileWriter(sourcesFile); for (File source : sources) writer.write(bashQuote(source.getPath()) + " "); writer.write(moreOptions); writer.close(); } private static void scanForSources(File source, List<File> sources) { if (source.isFile() && source.getName().endsWith(".java")) sources.add(source); else if (source.isDirectory()) { File[] files = source.listFiles(); for (File file : files) scanForSources(file, sources); } } public static String backtick(String cmd) throws IOException { File outFile = File.createTempFile("_backtick", ""); File scriptFile = File.createTempFile("_backtick", ""); String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; //Log.info("[Backtick] " + command); try { saveTextFile(scriptFile.getPath(), command); String[] command2 = {"/bin/bash", scriptFile.getPath() }; Process process = Runtime.getRuntime().exec(command2); try { process.waitFor(); } catch (InterruptedException e) { throw new RuntimeException(e); } int value = process.exitValue(); //Log.info("exit value: " + value); return loadTextFile(outFile.getPath(), ""); } finally { scriptFile.delete(); } } /** possoibly improvable */ public static String bashQuote(String text) { if (text == null) return null; return "\"" + text .replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", "\\r") + "\""; } public final static String charsetForTextFiles = "UTF8"; /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static String loadTextFile(String fileName, String defaultContents) throws IOException { if (!new File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); return loadTextFile(inputStreamReader); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) builder.append(line).append('\n'); } finally { reader.close(); } return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); } } }} | 2015-08-20 00:52:05 | delete |
| Snippet ID: | #562 |
| Snippet name: | javax, v1 |
| Eternal ID of this version: | #562/1 |
| Text MD5: | b5179af2be0c6cd1d105cdd0d977bbae |
| Author: | stefan |
| Category: | |
| Type: | Java source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-04-10 16:51:32 |
| Source code size: | 6109 bytes / 157 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 1735 / 265 |
| Referenced in: | [show references] |