• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

how to compile java file from different root directory

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 42145
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic