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

java :hello world的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

  10. c#与java的区别

    经常有人问这种问题,用了些时间java之后,发现这俩玩意除了一小部分壳子长的还有能稍微凑合上,基本上没什么相似之处,可以说也就是马甲层面上的相似吧,还是比较短的马甲... 一般C#多用于业务系统的开发 ...

随机推荐

  1. 【循序渐进学Python】12.Python 正则表达式简介

    正表达式就是一段匹配文本片段的模式,在Python 中 re 模块包含了对正则表达式(regular expression)的支持. 1. 正则表达式的基本概念 1. 通配符 点号( . )可以匹配换 ...

  2. MVC分页之MvcPager使用

    最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax异步分页.废话不多说了直接上代码. 一.M ...

  3. 回文串---Best Reward

    HDU   3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...

  4. Python on VS Code

    install python extension Press F1, and input "ext install python". Then the icon at the le ...

  5. nginx配合modsecurity实现WAF功能

    一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...

  6. 防止用户误操作退出APP的处理

    /** * 软件退出的处理:先跳到第一个页面,再点提示“再点一次退出”,2秒内再点一次退出 * 防止用户误操作 */ private boolean isExist=false; private Ha ...

  7. asp.net 发送邮件

    asp.net 发送邮件 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();            msg.To. ...

  8. window下从硬盘安装linux系统iso镜像文件的方法

    首先,需要安装grub2win,http://sourceforge.net/projects/grub2win/ 其次,将iso文件放在grub2可识别的分区, 如c:\abc\iso.iso 最后 ...

  9. 从网络中获取图片显示到Image控件并保存到磁盘

    一.从网络中获取图片信息: /// <summary> /// 获取图片 /// </summary> /// <param name="url"&g ...

  10. offset笔记

    1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidth 5.offsetHeight offsetWidth是元素的可视宽度,这个宽度包括元素的边框 ...