• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to compile java file from different root directory

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

i'd like to know how to compile a java file from different root directory.

i tried -classpath option but it doesn't seem to be working .

help me out.

thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just type javac and then the path to the file. For example,

javac /usr/my/file/that/is/located/elsewhere/com/coderanch/MyClass.java

This will produce the file /usr/my/file/that/is/located/elsewhere/com/coderanch/MyClass.class
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You just type javac and then the path to the file. For example,

javac /usr/my/file/that/is/located/elsewhere/com/coderanch/MyClass.java

This will produce the file /usr/my/file/that/is/located/elsewhere/com/coderanch/MyClass.class


She/he will need to set the classpath to the all the root directories otherwise MyClass.java wouldn't find the files (classes) it needs in the other directories.

What I usually do is this:

cd /usr/my/file/that/is/located/elsewhere

javac -classpath RootDir1:RootDir2:RootDir3:jarfilepath1:jarfilepath2:$CLASSPATH com/coderanch/MyClass.java

on windows, it should look like:

javac -classpath RootDir1;RootDir2;RootDir3;jarfilepath1;jarfilepath2;%CLASSPATH% com/coderanch/MyClass.java


If I remember correctly javac will recursively compile the files it needs to compile com/coderanch/MyClass as well in the other root folders.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sometimes use scripts to set the classpath, basically:

setclasspath.sh
CLASSPATH=RootDir1:RootDir2:RootDir3:jarfilepath1:jarfilepath2:$CLASSPATH
export CLASSPATH

Then:
. .setclasspath #Notice the dot so CLASSPATH is applied to this shell
javac com/coderanch/MyClass.java
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:i'd like to know how to compile a java file from different root directory.

i tried -classpath option but it doesn't seem to be working .

Let's take your source code file from one of your other topicsLet's assume the source code file is located in the directory D:\ocajp\src\com\pack (as you can see I'm working on a Windows machine).

Now I can compile this source code file from the root directory D:\ usingAfter executing this command a Sample.class file is created in the directory D:\ocajp\src\com\pack. Now if you want to execute this .class file, you'll need to set the classpath to the appropriate directoryIf you use a wrong directory in the classpath, the JVM won't be able to find your .class file and you'll get an appropriate error message

Now if you want to seperate your source code files from the generated .class files (which is considered to be a good practice), you can use the option -d of the javac command. So if we want to have the .class files being generated in the directory D:\ocajp\classes, you'll need to execute this commandAs you can see from this error message, the destination directory (in this example: D:\ocajp\classes) must exist! It's not created by the javac command. So after I have created the directory D:\ocajp\classes manually, I can run the previous command againIt finishes without an error message and a Sample.class file is created in the directory D:\ocajp\classes\com\pack. Now if you want to execute this .class file, you'll need to set the classpath to the appropriate directory

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic