Java application executing other Java application using Ant on Mac OS X
I´m working on a client-server Java-application that is meant to work on Mac OS X as well as Windows and Linux. The application has several different client modules which should be executed from a launcher application. I have looked at the ProcessBuilder class, but i doesn't seem to be right for me. I found a thread here att stackoverflow where an example using Ant was suggested (here). I implemented a method that executes the client modules:
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 is an object that contains some information about the application to be launched. This code works well and the application is started. The problem occurs in the application that is launched where this row causes a ClassNotFoundException:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
This is the stack trace:
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)
If I try to run the a开发者_如何学运维pplication standalone it works fine. The launcher application contains the exact same row that is failing, and that works fine in the launcher. It seems like there is something that is not right with the ant-way of launching the application. I have compared the System.properties and the classpath when executing the application from the launcher and stand alone (which works) and there is no difference.
I just cannot see why this class can´t be found. Has anyone else see this problem? Any suggestion is appreciated!
Thanx!
These are some of the hardest errors to debug. It's a good sign that the code runs OK outside of Ant, which leaves a classpath issue. Check the classpath of your project, your system can't find "apple.laf.AquaLookAndFeel".
精彩评论