Compiling .java files from the command line - external libraries, classpath

30,561

Solution 1

Judging by your .classpath file, this probably works:

javac -d bin -cp libs/joda-time-2.2.jar src/your/package/*.java

This assumes you run it in your project's directory.

The -d flag tells javac where to put the output files. The -cp flag is a shorthand for -classpath. I got these parameters based on the .classpath file in your question.

In general you can figure out what is needed by reading and understanding the errors in the output of javac. I bet you got many cannot find symbol errors at first, because the "symbols" were not on your classpath, so javac could not possibly know how to find them.

It is very important to understand how to build your projects, it is fundamental knowledge and a core competency of programmers.

Once you understand how to build manually like this, I recommend to look into build tools, preferably Maven, alternatively Ant that make building on the command line much easier. The majority of Java projects use these tools to build their products on the command line and in automated build systems, continuous integration, and so on.

Solution 2

Eclipse basically compiles your programs and puts them in the bin folder. Whereas command line compiles them in the same directory itself. If you're compiling files in the src folder itself, you'll see that it's littered with .class files now. The default directory of the Java compiler in Eclipse is your project. For command line it's the folder in which your source files are located.

So, if you have a project called Project on your Desktop, then the default directory because of Eclipse for the source files in Project\src is C:\Users\UserName\Desktop\Project but if you compile it with command line then the default directory is C:\Users\UserName\Desktop\Project\src.

To obtain the current default directory programmatically, use this :

System.out.println("System.getProperty ("user.dir")");

Now, if you need to compile with command line, I would suggest you put the .jar file in your src folder.

However, I do not think you require the .classpath file. Here is what it is.

Share:
30,561
false_azure
Author by

false_azure

Updated on April 14, 2020

Comments

  • false_azure
    false_azure about 4 years

    I made a project in Eclipse and am now trying to ensure that the .java files compile from the command line. I've been trying to compile using javac *.java in the folder with all my .java files. However, this results in errors due to a reference to a class from an external library, Joda-time. I have the following .classpath file that Eclipse made for the project but don't know what to do with it.

        <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
        <classpathentry kind="lib" path="libs/joda-time-2.2.jar"/>
        <classpathentry kind="output" path="bin"/>
    </classpath>
    

    I tried compiling with javac -classpath *.java but this only generates more errors. My source files are inside a package folder inside a 'src' folder. Where do I put my classpath file and my joda-time-2.2.jar file, and how do i get everything to compile?

    [edit] I'm using Windows 7