2011-03-10 190 views
1

我想從幾百個jar文件中找到某個java類的位置。我想用批處理文件來完成它:如何從文本文件傳遞參數到Windows XP批處理文件?

首先我得到的所有jar文件的列表:

dir /s *.jar > jarfiles.txt 

然後創建一個bacth文件findclass.bat:

jar tf %1 

最後我跑這批像這樣:

findclass < jarfiles.txt 

但是這不起作用。

怎麼了,我該怎麼辦?

或者其他更好的方法來找到一個類比使用批處理?

回答

3

看到HELP FOR

,然後嘗試

FOR /F "delims=" %%A IN (jarfiles.txt) DO findclass %%A

或只是

FOR /R %%A IN (*.jar) DO findclass %%A

+2

如果'findclass'是第二個批處理文件,你會想'CALL findclass'所以它將控制權返回給第一個批處理文件。或者,如果findclass.bat只是一行代碼,那麼您可以放棄第二批文件並直接運行該行。 – aphoria 2011-03-10 15:43:45

+2

對!那麼該行就是'FOR/R %% A IN(* .jar)DO jar tf %% A' – 2011-03-10 16:09:06

+2

@ wang.chance:只是澄清:* PA *的最後一條建議意味着只有一個批處理文件做所有*工作。沒有'jarfiles.txt'和沒有'findclass.bat'。在我看來,這是一個完美的替代品。 – 2011-03-10 16:43:02

2

我認爲這應該工作,只需用你的類名稱替換<your class name>

@echo off 
set JARTEXTFILE=jarfiles.txt 

dir /S /B *.txt > %JARTEXTFILE% 

for /f "tokens=1,* delims=¶" %%A in ('"type %JARTEXTFILE%"') do (
    jar tf %%A | findstr /C:"<your class name>" 
) 
2

使用我的ClassFinder實用程序。它很簡單,快速,併爲此目的而建造!

http://www.adarshr.com/papers/classfinder

Usage: 

    java -jar cf.jar SEARCH [DIRECTORY] [OPTIONS]... 

Searches all JAR files in the current directory and its sub-directories for 
entries matching the SEARCH argument. If DIRECTORY is provided, searching will 
be done in that location instead of the current directory. 

SEARCH: 

    A search string containing the class name (entry name). Wild card (*) is 
    supported. Package separator can be either of `/', `\' or `.'. 

    Examples: 

    java.lang.String 
    java/util/ArrayList 
    java/lang/Str*B*er 

    If non ".class" entries also need to be searched, option -a (--all-types) 
    should be specified. Please see the OPTIONS section for a more detailed 
    explanation. 

DIRECTORY: 

    If this is not provided, current directory and all its sub-directories will 
    be used for performing the search. However, if this argument is provided, 
    the same and its sub-directories will be used as the location to fetch JAR 
    files from. 

    If a recursive scan is not needed, option -s (--shallow) can be specified. 

OPTIONS: 

    -h   --help 
        Shows this help 
    -o [path] --redirect-output 
        Redirect output to a file path supplied. 
    -x [x1,x2] --archive-extensions 
        Extensions in addition to the default ".jar". Comma or space 
        separated list accepted. 
    -i   --insensitive-case 
        Case insensitive search. 
    -q   --quiet 
        Silent search without the progress bar animation. 
    -a   --all-types 
        Removes the filtering on ".class" types so that other types 
        such as ".properties", ".xml", etc can also be searched for. 
    -s   --shallow 
        Performs a shallow search. Doesn't recurse. 

Examples: 

    java -jar cf.jar org/apache/log4j/Level D:\Frameworks 
    java -jar cf.jar *OracleDriver C:\oracle -x jar,zip 
    java -jar cf.jar messages.properties D:\IBM\WebSphere -a -x jar,war,ear 
    java -jar cf.jar util.* D:\Java -i -q 
相關問題