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