Register Login
DioEnglish.com Return Index

denver's Space http://www.dioenglish.com/?1008 [Favorites] [Copy] [Shares] [RSS]

Blogs

A trap of Runtime.getRuntime().exec

439 views. 2010-1-28 17:14 |Individual Classification:Tech|

Description: When you use Runtime.getRuntime().exec to implement a Java command line on various platforms, your codes may work well on Windows but failed on Linux. Exception "ClassNotFound" occurs.

Analysis: Sample is below(highlighted in red. I don't know why hasn't coding-style button here?):
package com.ibm.lotus.search.engine.test.cluster;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClusterRunner
{
    ...
        public boolean startNode(NodeInfo nodeinfo)
        throws IOException
    {
        NodeInfo nodeinfo1 = nodeinfo;
        JVM INSTR monitorenter ;
        ...
        String s = (new StringBuilder()).append("\"").append(System.getProperty("java.class.path")).append("\"").toString();   //problem codes highlighted by red Bold
        String s1 = (new StringBuilder()).append("java -classpath ").append(s).append(" ").append(nodeinfo.getClassName()).append(" ").append(nodeinfo.getPortNumber()).append(" ").append(configFolderPath).toString();
        ...
        Process process = Runtime.getRuntime().exec(s1);
        ...


Linux JRE, neither SUN or IBM support to use a pair of quota marks to enclose a Java classpath string, though it does make sense for Win.

Solution: so, perhaps you have to add some branches do handle these command string, for example:
if (File.seperator == '\') {
     //codes for Win
}else{
     //codes for *nix
}

(note: people can implements a Java command line which contained a classpath enclosed by quota marks on a Linux Shell prompt, just because the quota marks be implicitly trimmed by Shell)

Post comment Comment (3 replies)

Reply bonnie8934 2010-1-28 19:56
are you a prof?
Reply denver 2010-1-28 21:09
No, I am just an engineer. I am very happy for you reading this, it's difficult to find another developer here, it's so surprise.
This is an issue I met, I resolved it after some tribulation, welcome you to review and give some comments.
Reply minyingtao 2010-1-29 13:08
Can't understand these

facelist doodle 涂鸦板

You need to login first Login | Register

每周一篇英文日志,坚持一年,你的英语能力将发生质的飞跃!

DioEnglish.com --- A Nice Place to Practice English and Make New Friends!

English Writing, English Blog, English Diary, 英语角, 英语写作, 英文写作, 英语交流, 英语日记, 英语周记, 英文日记, 英语学习, 英语写作网, 英语作文大全

Website Rules|Contact Us|茶文化|英文博客网 ( 京ICP备06064874号-2 )

GMT+8, 2024-5-20 04:18

Powered by DioEnglish.com

© 2008-2013 China English Blogs

Top