练习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();
}
}
}

最新文章

  1. Postgresql扩展及UUID
  2. ZOJ 3911 线段树
  3. Eclipse中启动tomcat报错:A child container failed during start
  4. codeforces 490C. Hacking Cypher 解题报告
  5. ThinkPHP整合微信支付之发裂变红包
  6. JavaScript设计模式与开发实践 - 策略模式
  7. makefile--模式规则(七)
  8. XML模式:Dublin Core
  9. 数据库合并数据sql
  10. Apache配置详解【转】
  11. JS中的普通函数和箭头函数
  12. Java 工具类—日期获得,随机数,系统命令,数据类型转换
  13. BZOJ 1072: [SCOI2007]排列perm [DP 状压 排列组合]
  14. Intellij IDEA 修改jsp 不能实时更新
  15. Linux系统初始化配置项(centos7)
  16. [ArcGIS]ArcGIS Server环境搭建,发布服务,以及使用ArcGIS API for JavaScript
  17. 005-mac下Java开发工具安装,idea,maven,git,node
  18. SVN的Not authorized to open root of edit operation解决办法
  19. TabLayout+ViewPager 标题不显示问题
  20. Android 访问 Webapi 更新UI

热门文章

  1. js定时器调用参数的方法
  2. csharp: WebBrowser read baidumap
  3. 【Asphyre引擎】学习笔记(二)
  4. php学习笔记:foreach循环访问关联数组里的值
  5. Linux 学习手记(4):Linux系统常用Shell命令
  6. ASP.NET本质论第二章应用程序对象学习笔记1
  7. andriod arcgis加载影像TIF
  8. 2015年第2本(英文第1本):《The Practice of Programming》
  9. UIAlertController弹出提示框
  10. ARC-数据类型需要释放的情况