Adrian
Adrian

Reputation: 73

Compile Multiple Classes and pass command line arguments

How would I compile two classes using command line(without the use of additional software) and pass arguments to it?

I've created myself a sources.txt file which contains definitions where each class is. I did this by using the following command

dir /s /B *.java > sources.txt

Then I try to do javac @sources.txt although that does not help as I'm getting the following error:

error: invalid flag: C:\Users\Adrian
Usage: javac <options> <source files>
use --help for a list of possible options

Additionally, my path does contain one space, after my username. Adrian $. So in sources.txt it looks like this: C:\Users\Adrian $\

I did try putting quotes around them or percentage sign but then I get error which specifies that the file was not found.


Full Main code:

package me.adrian;

import java.io.FileNotFoundException;

public class Main {

    public static void main(String[] args) {
            CSVoperator CSVfile = new CSVoperator();

            try {
                CSVfile.readCSV(args); //get args into there.
            }
            catch (FileNotFoundException e){
                e.printStackTrace();
            }
    }

}

Upvotes: 1

Views: 711

Answers (2)

Karol Dowbecki
Karol Dowbecki

Reputation: 44942

Since your paths contain space they has to be quoted. You can do it by iterating over sources.txt in a loop and quoting the entire line:

FOR /F %%i IN (sources.txt) DO "javac %%i"

or you can try dir /x to generate sources.txt with a short path as per dir docs

/x

Displays the short names generated for non-8dot3 file names. The display is the same as the display for /n, but the short name is inserted before the long name.

One way or another, scripting in Windows Batch sux.

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 39998

You can compile directly by specifying multiple names in on command

javac file1.java file2.java

or by using *, all .java files that are in current directory will be compiled

javac *.java

Upvotes: 1

Related Questions