// -*- java -*- // smallest // the smallest running program doing nothing public class smallest { public static void main(String[] args) { } } // hello world // print a simple string on stdout public class hello_world { public static void main(String[] args) { System.out.println("Hello World"); } } // argv // access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) public class argv { public static void main(String[] args) { if (args.length > 0) System.out.println(args[0]); } } // env // access environment variable public class env { public static void main(String[] args) { System.out.println(System.getenv("HOME")); } } // test file exists // return exit code error (non zero) if a file does not exist public class exists { public static void main(String[] args) { if(!(new java.io.File("/etc/mtab")).exists()) System.exit(1); } } // test file readable // return exit code error (non zero) if a file is not readable public class readable { public static void main(String[] args) { if(!(new java.io.File("/etc/mtab")).canRead()) System.exit(1); } } // formatting // print integers in a simple formatted string public class formatting { public static void main(String[] args) { int a=1, b=2; System.out.println("" + a + " + " + b + " = " + (a + b)); } } // system // call an external program and check the return value import java.io.*; public class system { public static void main(String[] args) throws Exception { if (Runtime.getRuntime().exec("false").waitFor() != 0) System.err.println("false failed"); BufferedReader input = new BufferedReader(new InputStreamReader( Runtime.getRuntime().exec("echo done").getInputStream() )); String line; while ((line = input.readLine()) != null) System.out.println(line); } } // sed in place // remove #-comments from a file (modifying the file, i.e. in place) import java.io.*; public class uncomment { public static void main(String[] args) { try { FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); File f = new File(args[0] + ".tmp"); FileWriter fw = new FileWriter(f); String line = ""; while ((line = br.readLine()) != null) { int pos = line.indexOf('#'); if (pos > -1) line = line.substring(0,pos); fw.write(line + '\n'); } fr.close(); fw.close(); f.renameTo(new File(args[0])); } catch(Exception ex) { ex.printStackTrace(); } } } // compile what must be // find and compile .c files into .o when the .o is old or absent import java.io.*; public class compile { public static void main(String[] args) { filter ft = new filter(); if (args.length > 0) { File f = new File(args[0]); File[] cs = f.listFiles(ft); for (int i = 0; i < cs.length; i++) tryFile(cs[i]); } } static void tryFile(File c) { String o = c.toString().replaceAll(".c$", ".o"); File tmp = new File(o); if (!tmp.exists() || tmp.lastModified() < c.lastModified()) { System.out.println("compiling " + c.toString() + " to " + o); String cmd = "gcc -c " + c.toString() + " -o " + o; try { java.lang.Runtime.getRuntime().exec(cmd); } catch(Exception ex) { ex.printStackTrace(); } } } } class filter implements FileFilter { public filter() { } public boolean accept(File f) { return f.toString().endsWith(".c"); } }