Java进程通讯及gcj将jar转为exe

Main.java
/**
 * 管道进程通讯, to exe.jar
 * @author RuiLin.Xie - xKF24276
 *
 * 找时间扩展为可传对象
 */
public class Main
{

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException
	{
		//执行程序
		Process process = Runtime.getRuntime().exec("java -cp c:/exe.jar; com.java.exe.Exe");
		
		//发送消息
		OutputStream out = process.getOutputStream();
		out.write("10000".getBytes());
		out.flush();
		//必需关闭输入流后才能得到返回消息
		out.close();
		
		//jdk1.5以上版本
		//BufferedInputStream buf = new BufferedInputStream(process.getInputStream());
        //Scanner s = new Scanner(buf);
		//jdk1.4版本
		//读取程序返回消息
		InputStreamReader isr = new InputStreamReader(process.getInputStream());
		BufferedReader s = new BufferedReader(isr);
		String line = null;
		StringBuffer all = new StringBuffer();
		while((line = s.readLine()) != null)
		{
			all.append(line);
		}
		s.close();
		isr.close();
		
		//显示返回消息
		System.out.println(all.toString());
	}



Exe.java
/**
 * 模仿exe程序,导出为jar包放到 c:\exe.jar
 * @author RuiLin.Xie - xKF24276
 *
 */
public class Exe
{

	public static void main(String[] args) throws IOException
	{
		//准备接收父程序消息
		InputStream in = System.in;
		InputStreamReader isr = new InputStreamReader(in);
		BufferedReader s = new BufferedReader(isr);
		String line = null;
		StringBuffer all = new StringBuffer();
		
		//接收父程序消息
		while((line = s.readLine()) != null)
		{
			all.append(line);
		}
		s.close();
		isr.close();
		//当父程序关闭输入流时,执行操作
		
		//执行对应任务
		int cmd = Integer.parseInt(all.toString());
		String ret = null;
		if(cmd == 10000)
		{
			ret = sayHello(cmd);
		}
		else
		{
			ret = errorCmd(cmd);
		}
		
		//返回消息给父程序
		OutputStream out = System.out;
		out.write(ret.getBytes());
		out.flush();
		out.close();
	}
	
	/**
	 * 说hello
	 * @param name
	 * @return
	 */
	public static String sayHello(int cmd)
	{
		return "hello. success :" + cmd;
	}
	
	/**
	 * 找不到命令
	 * @param entity
	 * @return
	 */
	public static String errorCmd(int cmd)
	{
		return "cmd not found :" + cmd;
	}

}


参考文档:

GCJ编译java程序的头痛问题
http://www.cnblogs.com/huqingyu/archive/2004/11/13/63317.html

将java库转换为.net库(转载)
http://www.cnblogs.com/chegan/archive/2005/10/11/252103.html

JAVA IO之管道流总结大全
http://blog.chinaunix.net/u1/55983/showart_521503.html

共享内存在Java中的实现和应用
http://www.ibm.com/developerworks/cn/java/l-memshare/index.html

你可能感兴趣的:(java,html,c,.net,IBM)