0

I am trying to compile an application from the command line

The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . in a package name.

Does anyone have any ideas on what i need to do differently?

Thanks

rik
  • 1,279
  • 4
  • 20
  • 29
  • 3
    it seems. these are the classpath issues, IDE manages it for you , from terminal you need to do it – jmj Aug 04 '11 at 11:32
  • @rik: Check your class path. Do you have include all needed jars and packages in your class path? – Harry Joy Aug 04 '11 at 11:33
  • Besides that I'd rather use a build tool like ant or maven for command line compiling. – Thomas Aug 04 '11 at 11:34

2 Answers2

4

Your classpath is not set up correctly. Look at your Eclipse project in the .classpath file. In there you will find a lot of classpathentry elements. You will need to replicate this for your command line compilation.

To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions.

You can also use a build tool called ant to automate this for you.

Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47
0

I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).

A command line for compiling a Java app which dependes on Log4j might look like this:

javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java

If you have multiple classpath entries you need to separate them with a semicolon.

For ease of use you could create a startup script. This can be a simple batch script or a more elaborate ant script (which requires installing ant).

This is only the tip of the iceberg known as 'classpath hell'.

EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • Agreed, and in practice you'd almost certainly set the CLASSPATH in the context of a build script. Of course the ultimate goal would be to use Maven, and its great dependency management system, to alleviate the 'classpath hell' you refer to. – Andrew Fielden Aug 04 '11 at 12:16