I usually work within Eclipse, but sometimes I get questions like can you run an application built with Eclipse from the command line? And you can. It's pretty straightforward, and I don't know if I'm doing it in the best way. But here is what I did. I have two examples.
The first is just a Hello World. I'm using Eclipse 3.6M4 on the Mac. I made a Java project with File-> New -> Java Project. I called the project com.ted.hello. I made an empty package in the project and then added a class called Hello. Run it as a Java Application from within Eclipse, and it looks like the following. It prints out “hello java” to the console.
My workspace is /Users/ted/EclipseProjects/work36M4RCPA. Don't ask why the workspace is called that. I was just trying out some RCP applications earlier and used an existing workspace.
To run this Hello Java program from the command line, go into the directory /Users/ted/EclipseProjects/work36M4RCPA/com.ted.hello/bin. The structure under bin looks like
newtricks:bin ted$ find .../com./com/ted./com/ted/hello./com/ted/hello/Hello.classnewtricks:bin ted$
Run the application as
newtricks:bin ted$ java com.ted.hello.Hellohello javanewtricks:bin ted$
Yes, I wanted to use Apache Log4j. This lets me address the classpath issue ... like finding the log4j.properties file and the log4j jar. Here's how it looks when run from within Eclipse.
I'm using the 1.2.15 version of log4j, which is the stable version that most people use. You download it in a zip that contains the log4j-1.2.15.jar. The later versions are alpha and beta, and you have to get them from apache's svn repository and build it yourself.
Add log4j-1.2.15.jar to the project's Java Build Path. Right click the project, choose Properties, choose Java Build Path, select the Libraries tab, click the Add External JARs button, and browse to the jar's location. I also had to tell the project how to find log4j.properties. I did this by adding a resource filter. Right click the project, choose Properties, choose Resource Filters, and click the Add button. Select the radio buttons Include and Files. Then, type log4j.properties in the Pattern: textbox.
To run this Logging program from the command line, go into the directory /Users/ted/EclipseProjects/work36M4RCPA/com.ted.log/bin. The structure under bin looks like
newtricks:bin ted$ find .../com./com/ted./com/ted/log./com/ted/log/Logging.class./log4j.propertiesnewtricks:bin ted$
I have to set my CLASSPATH variable to point to the log4j.jar and my current directory (which is where the log4j.properties is). Mac has the bash shell so here is the command for that (I originally forgot to export).
newtricks:bin ted$ CLASSPATH=/Users/ted/Downloads/Log4j/apache-log4j-1.2.15/log4j-1.2.15.jar:.newtricks:bin ted$ export CLASSPATH
newtricks:bin ted$ env |grep CLASSCLASSPATH=/Users/ted/Downloads/Log4j/apache-log4j-1.2.15/log4j-1.2.15.jar:.
newtricks:bin ted$ pwd/Users/ted/EclipseProjects/work36M4RCPA/com.ted.log/bin
newtricks:bin ted$ java com.ted.log.Logging0 [main] INFO com.ted.log.Logging - hello logwrite hello1 [main] INFO com.ted.log.Logging - goodbye log
So running the Java application form the command line is as you would suspect pretty straightforward. You just have to be in the right directory, use the navigation from your package, and in the case of the second example, set the classpath correctly.
Remember Me
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010, Ted Kubaska
E-mail