java :hello world
练习java的基本语法。
output hellow world.
需求:打包自身项目的bin目录文件为一个临时可运行的jar文件,执行完后删除。
使用process执行jar文件,返回输入流和错误流的信息。
熟悉了java –cp jarname.jar , java –jar jarname 等命令的使用。
生成可执行jar包和非可执行jar包的区别就在于是否在manifest中指明main class.
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
class StreamConsumer extends Thread {
InputStream is;
String type;
StreamConsumer(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
while (line != null && !line.startsWith(type)) {
System.out.println(type + ">" + line);
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class helloworld {
static File createTempJar(String root) throws IOException {
if (!new File(root).exists()) {
return null;
}
Manifest manifest = new Manifest();
// generate a running jar
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue("Class-Path", ".");
manifest.getMainAttributes().putValue("Main-Class",
helloworld.class.getName());
final File jarFile = File.createTempFile("tmpJob-", ".jar", new File(
System.getProperty("java.io.tmpdir")));
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
jarFile.delete();
}
});
JarOutputStream out = new JarOutputStream(
new FileOutputStream(jarFile), manifest);
createTempJarInner(out, new File(root), "");
out.flush();
out.close();
return jarFile;
}
static void createTempJarInner(JarOutputStream out, File f, String base)
throws IOException {
if (f.isDirectory()) {
base = base.length() > 0 ? base + "/" : base;
for (File _file : f.listFiles())
createTempJarInner(out, _file, base + _file.getName());
} else {
out.putNextEntry(new JarEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int n = in.read(buffer);
while (n != -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
out.closeEntry();
in.close();
}
}
public static void main(String[] args) {
args = System.getProperty("java.io.tmpdir").split(" ");
for (String arg : args)
System.out.println(arg);
System.out.println("hello world!");
try {
File jarfile = createTempJar("bin");
if (!jarfile.exists())
System.out.print("jar file is not exists");
else {
String mycmd = "java -jar " + jarfile.toString();
// "java -cp .;" + jarfile.toString() ;
// + " " + helloworld.class.getName();
System.out.println("command:" + mycmd);
Process proc = Runtime.getRuntime().exec(mycmd);
StreamConsumer sc = new StreamConsumer(proc.getInputStream(),
"output");
StreamConsumer scerror = new StreamConsumer(
proc.getErrorStream(), "error");
sc.start();
scerror.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}