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

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.