Member Avatar for iamthwee

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.

ThanQ

Recommended Answers

All 5 Replies

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

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

Ok I still can't do this?

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

c:\j2sdk1.4.2_04\bin

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
:cry:

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.
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  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
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.