Thursday, 19 January 2017

java - Compile all files in src?



Here's what I've got:




/myjava/compile.cmd
/myjava/src/a/HelloWorld.java
/myjava/src/b/Inner.java

/myjava/src/b/Inner2.java
/myjava/bin


HelloWorld:




package a_pack;

import b_pack.Inner;

import b_back.Inner2;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World");

Inner myInner = new Inner();
myInner.myInner();


Inner2 myInner2 = new Inner2();
myInner2.myInner();

}

}


Inner.java





package b_pack;

public class Inner {

public void myInner() {
System.out.println("Inner Method");
}


}


Inner2.java




package b_pack;

public class Inner2 {


public void myInner() {
System.out.println("SecondInner");
}

}


Now what I'm trying to do is compile this so I can run.



I could compile it with:




javac -d bin src/a/HelloWorld.java src/b/Inner.java src/b/Inner2.java



But I want to use a generic command that doesn't require listing every subfolder.
How do I do this?


Answer



Since your HelloWorld class imports references to the Inner and Inner2 classes, you can use javac sourcepath flag to compile all the three classes :



javac -d bin -sourcepath src src/a/HelloWorld.java


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...