Home > database >  Compile source files to a different directory and then accessing them in another directory, how? JAV
Compile source files to a different directory and then accessing them in another directory, how? JAV

Time:10-24

My directory structure:

C:\jaBHa

here I want to create two packages - sources and classes.

so it looks like

C:\jaBHa\classes

C:\jaBHa\sources

the sources package must contain all the source files i.e., the .java files.

And then I want them to compile to the classes package which would contain all the .class files.

here are the codes for the three classes...

// MyDate.java
package classes;

public interface MyDate {
    void showDate();
}

// DateImpl.java
package classes;

import java.util.*;
import classes.*;
public class DateImpl implements MyDate {
    public void showDate() {
        Date d = new Date();
        System.out.println(d);
    }
}

// Test.java
package classes;

import classes.*;
public class Test {
    public static void main(String[] args) {
        DateImpl d = new DateImpl();
        d.showDate();
    }
}

Now, I know that there's some issue with my class DateImpl java file, but my objective is...

that I want to compile these java files using the commands

C:\jaBHa> javac -d C:\jaBHa\sources MyDate.java

C:\jaBHa> javac -d C:\jaBHa\sources DateImpl.java

C:\jaBHa> javac -d C:\jaBHa\sources Test.java

with this the MyDate.java, DateImpl.java and Test.java files will compile to classes package...

and then i run it by typing

C:\jaBHa> java classes.Test

as the class file of Test.java is in C:\jaBHa\sources\classes package...

but here's the problem, it compiles to C:\jaBHa\sources\ here it will create a package classes but I want it to compile to C:\jaBHa\classes not C:\jaBHa\sources\classes...

so I have to keep my all java files in C:\jaBHa\ so that it can compile to classes package.

CodePudding user response:

The -d option specifies the output directory for class files.

Try this.

C:\jaBHa>javac -d . sources\*.java

C:\jaBHa>java classes.Test
Mon Oct 24 19:19:05 JST 2022

C:\jaBHa>
  • Related