yuicompressor 文件名、目录名或卷标语法不正确

初次接触YUI Compressor,看起来还不错,应该是一个比较好的js压缩工具,也是css压缩工具,于是去github上下载了下来,下载地址也附上:

https://github.com/yui/yuicompressor/releases

下载的是最新的版本 YUICompressor 2.4.8 Released。也可以直接用这个地址进行下载:

https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar

下载之后将文件放置 D:\tools 下面,然后cmd命令行执行:

java -jar D:\tools\yuicompressor-2.4.8.jar
显示了相关语法信息:
YUICompressor Version: 2.4.8

Usage: java -jar yuicompressor-2.4.8.jar [options] [input file]

Global Options
  -V, --version             Print version information
  -h, --help                Displays this information
  --type <js|css>           Specifies the type of the input file
  --charset <charset>       Read the input file using <charset>
  --line-break <column>     Insert a line break after the specified column number
  -v, --verbose             Display informational messages and warnings
  -o <file>                 Place the output into <file>. Defaults to stdout.
                            Multiple files can be processed using the following syntax:
                            java -jar yuicompressor.jar -o '.css$:-min.css' *.css
                            java -jar yuicompressor.jar -o '.js$:-min.js' *.js

JavaScript Options
  --nomunge                 Minify only, do not obfuscate
  --preserve-semi           Preserve all semicolons
  --disable-optimizations   Disable all micro optimizations

If no input file is specified, it defaults to stdin. In this case, the 'type'
option is required. Otherwise, the 'type' option is required only if the input
file extension is neither 'js' nor 'css'.
上面一切操作顺利,下面来看看如何使用。

cmd执行命令:

java -jar D:\tools\yuicompressor-2.4.8.jar temp.css -o temp.min.css
原以为能正常输出结果,没想到马上就出错了:
java.io.FileNotFoundException: temp.min.css:\temp.css (文件名、目录名或卷标语法不正确。)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:208)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:21)
这是怎么回事呢?网上有的人说是2.4.8版本的bug,说要用回2.3.6就能正常了。但这样也太不科学了!

经过进一步的研究,将 -o 修改成 > 的符号,竟然就正常了!

java -jar D:\tools\yuicompressor-2.4.8.jar temp.css > temp.min.css
这次temp.css压缩成功。有时候,解决问题就是这么简单!任何时候,都不要放弃寻找最正确的路径。

您可能还会对下面的文章感兴趣: