How to run javac with paths as argument that contain white spaces?

放肆的年华 提交于 2019-12-10 14:03:38

问题


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


回答1:


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.




回答2:


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:


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




回答4:


  • First using the cd command in shell shift your directory to the one where your file is saved.

    cd /home/sayantani/PERSONAL\ FILES/sem\ 4\ courses/PLC/code/
    
  • Note that I've used "\" whenever there is space involved. "PERSONAL FILES" becomes "PERSONAL\ FILES".

  • Then use "javac filename.java"

    javac hello1.java
    

This should fix your problem. Note that doing "javac" on the entire path from the default directory isn't working.(for me)




回答5:


You need to escape spaces.

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




回答6:


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




回答7:


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" ...



回答8:


From usage info for "java /?"

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>cd "C:\Program Files (x86)\Java\jre6\bin"

C:\Program Files (x86)\Java\jre6\bin>java.exe
Usage: java [-options] class [args...]
       (to execute a class)
or  java [-options] -jar jarfile [args...]
       (to execute a jar file)

where options include:
-client       to select the "client" VM
-server       to select the "server" VM
-hotspot      is a synonym for the "client" VM  [deprecated]
              The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
              A ; separated list of directories, JAR archives,
              and ZIP archives to search for class files.

-classpath indicates that you need to use a semi-colon (";") for multiple paths.




回答9:


I can't test it but I'd suggest the following (as dmcgil suggested semicolon should be classpath separator on windows):

javac -Xlint:unchecked -classpath C:\Users\a^ b\workspace\;C:\Users\a^ b\workspace\files_to_compile

It seems that the escape charachter for win shell is caret.

That is also suggested here.

EDIT:

Also, in your question, I noticed usage of slashes (/) in paths, doesn't all versions of windows use backslashes(\) as file separators? I saw your comment somewhere on this thread stating just that, so I'll suppose you typoed in question.



来源:https://stackoverflow.com/questions/8934946/how-to-run-javac-with-paths-as-argument-that-contain-white-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!