How to run javac with paths as argument that conta

2019-06-22 07:20发布

I am trying to run the following

javac -Xlint:unchecked -classpath C:/Users/a b/workspace/ @C:/Users/a b/workspace/files_to_compile

but I'm getting a

javac: invalid flag C:/users/a

I've also tried to surround both paths with double quotes but it doesn't seem to help a bit:

javac -Xlint:unchecked -classpath "C:/Users/a b/workspace/" @"C:/Users/a b/workspace/files_to_compile"

What am I doing wrong? This same code worked correctly in other computers (probably because they didn't have any white space in their paths..).

Thanks

标签: windows javac
9条回答
The star\"
2楼-- · 2019-06-22 07:52

I've finally come up with the solution to the issue, and I guess no one here could have guessed it.

The cue to the answer lies with the fact that the contents of the files list (signaled as @ in the args) generally will have each one of its strings with the initial substring equal to what one passes as both the class path and the @ file.

so..

The trouble was never the command line parameters, as suggested, but with the contents of the @ file.

Each line of the file must be put in its own line, surrounded by quotes, and having into consideration that if you're in windows, you have to put the file names in the form of C:\\a\\b\\c.txt!!!

查看更多
地球回转人心会变
3楼-- · 2019-06-22 07:54

Your second try is right

javac -Xlint:unchecked -classpath "C:/Users/a b/workspace/" @"C:/Users/a b/workspace/files_to_compile"

But to be complete, you have to escape the spaces into the text file "files_to_compile" by using:

  • the same syntax as properties file : \

or

  • double quote each line

I suggest the second but I'm not sure.

查看更多
时光不老,我们不散
4楼-- · 2019-06-22 07:55

Its taking only the 1st part of the Source String remove the space between a b from the path and it should work fine C:/Users/a_b/workspace/" @"C:/Users/a_b/workspace/files_to_compile" . Never you should have spaces in the path else the latter part will be ignored by the compiler or else you can put a '\' between a\ b

查看更多
霸刀☆藐视天下
5楼-- · 2019-06-22 07:56

Bit of a hack, but if you're on Windows 7 you can get around this using the mklink utility to create another folder pointing to the same place, but without spaces.

Edit: perhaps a better solution:

cd "C:/Users/a b/"
javac ... -classpath "Workspace" ...
查看更多
劫难
6楼-- · 2019-06-22 08:00

You need to escape spaces.

Put a \ in front of each space and try that.

查看更多
The star\"
7楼-- · 2019-06-22 08:02

I have to admit this was more difficult than I had imagined. After some trial and error I came up with the following:

C:\lol>"C:\Program Files\Java\jdk1.7.0_07\bin\javac" -cp "c:\lol\a b;c:\lol\foo bar" Lol.java

where the folder structure is like:

./foo bar
./foo bar/Moo.java
./Lol.java
./a b
./a b/AB.java

I made an archive with the folders and the java files, which you can grab at: http://www.pvv.ntnu.no/~rakhmato/tmp/lol.tar

You should ignore the @ option because it is enough to give the compiler one file and a proper class path, it can figure out where everything is on its own. Just give the compiler your Main.java and it will figure out what that file depends on.

I would also recommend you to write a .bat script of sorts to make things simpler. Nothing fancy, something like this:

compile.bat:

"C:\Program Files\Java\jdk1.7.0_07\bin\javac" -classpath "c:\lol\a b;c:\lol\foo bar" Main.java

..put that in your project folder and run compile.bat from CMD

查看更多
登录 后发表回答