-5

I usually save my .java source files in the bin folder in the jdk directory. How can I change the place where my .java files are saved and compiled? For example, my documents folder?

  • 3
    Probably the worst possible place. Put `bin` directory in your **path** and you would be able to call `javac` fron anywhere. – PM 77-1 Aug 19 '14 at 01:12
  • Try using Google before asking. – Rudolf Real Aug 19 '14 at 01:15
  • learn ant, maven, or gradle, or even, heck, _make_. – bmargulies Aug 19 '14 at 01:16
  • possible duplicate of [How can I set the PATH variable for javac so I can manually compile my .java works?](http://stackoverflow.com/questions/2079635/how-can-i-set-the-path-variable-for-javac-so-i-can-manually-compile-my-java-wor) – Pranav Singh Aug 19 '14 at 07:15

4 Answers4

4

javac compiles files relative to its current working directory. You need to set the path (you'll want to change the location given in that answer to your actual JDK location). Then, you can have your command prompt's working directory in the location where you want your .java files to be compiled from, and simply call

javac MySource.java

You can also use an IDE such as Eclipse or IntelliJ IDEA, which will keep your files organized into projects, and build files in the correct locations. This is highly encourages as you'll get:

  • Project organization and building without having to worry about the internals
  • Integration of libraries into testing without having to remember a growing command line
  • Visual feedback for syntax errors (and some logic issues as well)
  • Step-by-step debugging of your code to figure out what went wrong.
Community
  • 1
  • 1
nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

Are you compiling by command-line or with an IDE? If you are compiling by command-line with javac, there is a -d option that controls the destination of class files. Here is what the manpage says about that.

-d directory
Sets the destination directory for class files. The destination directory 
must already exist; javac will not create the destination directory.  If a class 
is part of a package, javac puts the class file in a subdirectory reflecting the 
package name, creating directories as needed.  For example, if you specify 
-d /home/myclasses and the class is called com.mypackage.MyClass, then the class 
file is called /home/myclasses/com/mypackage/MyClass.class.  If -d is not specified, 
javac puts the class file in the same directory as the source file. Note: The 
directory specified by -d is not automatically added to your user class path.

If you are using an IDE, can you let us know which one?

11th Hour Worker
  • 337
  • 3
  • 14
1

First you need to specify what IDE you are using. If none, just copy the files to the desired directory and, when compiling, cd for that directory and use javac to compile it there.

..Maybe your problem is that you forget to include the full path to javac when compiling (because when you keep the sourcecode in the bin folder, the files to compile are in javac's local directory).

Alexis R Devitre
  • 288
  • 2
  • 6
  • 21
0

If you use Maven to initialize the project, the structure will be organised for you, including java source folders:

Standard Maven Directory Layout

src/main/java Application/Library sources

src/main/resources Application/Library resources

src/main/filters Resource filter files

src/main/config Configuration files

src/main/scripts Application/Library scripts

src/main/webapp Web application sources

src/test/java Test sources

src/test/resources Test resources

src/test/filters Test resource filter files

src/it Integration Tests (primarily for plugins)

src/assembly Assembly descriptors

src/site Site

LICENSE.txt Project's license

NOTICE.txt Notices and attributions required by libraries that the project depends on

README.txt Project's readme