我正在研究可以在Mac OS X以及Windows和Linux上工作的客戶端服務器Java應用程序。該應用程序有幾個不同的客戶端模塊,應該從啓動器應用程序執行。 我曾看過ProcessBuilder類,但我似乎並不適合我。我在這裏找到了一個線程att stackoverflow,其中提供了一個使用Ant的示例(here)。 我實現執行該客戶端模塊的方法:在Mac OS X上使用Ant執行其他Java應用程序的Java應用程序
public void launchAnt(ApplicationData applicationData) {
Project project = new Project();
project.setBaseDir(new File(System.getProperty("user.dir")));
project.init();
DefaultLogger logger = new DefaultLogger();
project.addBuildListener(logger);
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
logger.setMessageOutputLevel(Project.MSG_INFO);
System.setOut(new PrintStream(new DemuxOutputStream(project, false)));
System.setErr(new PrintStream(new DemuxOutputStream(project, true)));
project.fireBuildStarted();
System.out.println("ApplicationLauncher.launch(): Running");
Throwable caught = null;
try {
/**
* Create Java task
*/
Java javaTask = new Java();
javaTask.setTaskName("Run " + applicationData.getApplication().name());
javaTask.setProject(project);
javaTask.setFork(false);
javaTask.setFailonerror(true);
javaTask.setClassname(applicationData.getClassName());
/**
* Working directory
*/
File workdir = new File(System.getProperty("user.dir"));
if(workdir.exists()) {
javaTask.setDir(workdir);
} else {
System.out.println("ApplicationLauncher.launch(): ERROR: Unable to set workdir, " + workdir.getAbsolutePath() + " does note exist.");
}
/**
* Classpath
*/
Path path = new Path(project);
Collection<String> classpaths = getClasspath();
for (String classpath : classpaths) {
Path currPath = new Path(project, new File(classpath).getAbsolutePath());
path.add(currPath);
}
System.out.println("ApplicationLauncher.launch(): Classpath: " + path.toString());
javaTask.setClasspath(path);
/**
* Arguments
*/
Argument arg = javaTask.createArg();
arg.setValue(applicationData.getArguments());
/**
* Initiate and execute
*/
javaTask.init();
int ret = javaTask.executeJava();
System.out.println("ApplicationLauncher.launch(): Java task return code: " + ret);
} catch (BuildException e) {
caught = e;
}
project.log("ApplicationLauncher.launch(): Finished");
project.fireBuildFinished(caught);
}
的ApplicationData是一個包含有關要啓動的應用程序的一些信息的對象。此代碼運行良好,應用程序啓動。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
這是堆棧跟蹤:
java.lang.ClassNotFoundException: apple.laf.AquaLookAndFeel
at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1361)
at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1311)
at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1070)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:242)
at javax.swing.SwingUtilities.loadSystemClass(SwingUtilities.java:1788)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:484)
如果我嘗試運行應用程序獨立位置它工作正常時啓動,其中該行會導致一個ClassNotFoundException的應用程序出現問題。啓動器應用程序包含完全相同的失敗行,並且在啓動器中工作正常。似乎有些東西與啓動應用程序的螞蟻方式不符。 我從啓動程序執行應用程序並獨立(工作)時比較了System.properties和classpath,並沒有區別。
我只是不明白爲什麼不能找到這個類。 有其他人看到這個問題嗎?任何建議表示讚賞!
Thanx!