Hello everyone.

At the moment I'm compiling all my .java files in the same directory where the javac program is. I know this is a bad idea.

How do I link it from the command line and let's say export the .class files to another directory of my choice.


I would recommend looking into Ant. It has a task called javac that should do what you're looking to do.

Yeah I'll probably use that later, especially if it supports easy to make jar executables however, in the mean time do you know how to do it from the command line.

Ok I still can't do this?

Let's say this is where my javac program is:


And this is where I have written my program

c:\java files\hello.java

How do I compile hello.java so that the class files appear in the same directory?

I assume these commands have something to do with it. Please help me

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
                  set a system property
                  enable verbose output
    -version      print product version and exit
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
                  enable assertions
                  disable assertions
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
                  load native agent library by full pathname
                  load Java programming language agent, see java.lang.instrument

p.s is this version of java j2sdk1.4.2 an old version. Should I update. The reason I say that is because I can't get the code for Scanner util to work and the graphics g

All you need to do is add the location of your javac program to the path variable of your computer. That way, you can execute javac from anywhere. Using this method, all you need to do is change to your project folder and execute "javac file.java". To add a location to your path variable, right-click My Computer, select Properties, select the Advanced tab, and select Environment Variables. There should be two path variables: one for your user and one for the system. If you use multiple users, add to the System path variable; otherwise, modify your User path variable. To add a location to the path variable, select path, click Edit, go to the end of the variable value, add a semi-colon (semi-colons seperate locations in the path variable), and add the location you want to add (c:\j2sdk1.4.2_04\bin in your case). After you click OK on all the dialog windows, close any remaining command windows, reopen them, and you now can access the programs in the "c:\j2sdk1.4.2_04\bin" folder from anywhere.

If you don't want to set the path variable, you can always compile by referring to the javac program by it's full name while in your project folder, such as:

c:\j2sdk1.4.2_04\bin\javac hello.java
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.