특정 문자열이 파일 목록에 포함되어 있는지 알아야합니다. 파일 목록은 동적이며 문자열의 동적 목록을 확인해야합니다. 결과를 처리하려면 Java에서 수행해야합니다 (true 또는 false). 그리고 MS Windows도 필요합니다.Java에서 GnuWin 명령을 호출하거나 더 좋은 방법이 있습니까?
find C:/temp | xargs grep import -sl
GnuWin은이 아무 문제없이 cmd를 작동합니다
나는 내가이 일을 유닉스 방법을 사용하려고 그 문제에 대해 생각했다. 그래서 이것을 자바 언어로 변환하려고했습니다. Runtime 클래스와 ProcessBuilder 클래스 사용에 관한 많은 기사를 읽었습니다. 그러나 팁 중 어느 것도 작동하지 않습니다.String binDir = "C:/develop/binaries/";
List<String> command = new ArrayList<String>();
command.add("cmd");
command.add("/c");
command.add(binDir+"find");
command.add("|");
command.add(binDir+"xargs");
command.add(binDir+"grep");
command.add("import");
command.add("-sl");
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File("C:/temp"));
final Process proc = builder.start();
printToConsole(proc.getErrorStream());
printToConsole(proc.getInputStream());
int exitVal = proc.waitFor();
나는 또한 명령을 CONCAT 많은 다른 방법을 시도
String binDir = "C:/develop/binaries/";
String strDir = "C:/temp/";
String[] command = {"cmd.exe ", "/C ", binDir + "find.exe " + strDir + " | " + binDir + "xargs.exe " + binDir + "grep.exe import -sl" };
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
printToConsole(proc.getErrorStream());
printToConsole(proc.getInputStream());
int exitVal = proc.waitFor();
하지만 중 I (예 :이 파일을 찾을 수 없습니다.) 오류 메시지가 : 마지막으로 나는 다음과 같은 두 가지 코드 조각을 시도 또는 과정이 결코 돌아 오지 않습니다.
내 질문 : 1.이 직업을하기에 더 좋은 방법이 있습니까? 2. 그렇지 않은 경우 : 코드에 오류가 있습니까? 3. 그렇지 않은 경우 : 해당 명령을 실행해야하는 다른 방법이 있습니까?
미리 감사드립니다. 내 머리의 상단에서
첫 번째 시도에서 strDir이 표시되지 않습니다. 오타입니까? –
아니요, 두 번째의 strDir은 첫 번째 파일에서'builder.directory (새 파일 ("C :/temp"));로 설정됩니다. –
strDir과 binDir을 혼동하지 않으십니까? 어쨌든, personnally 나는 오히려 자바 코드와 필요하다면 당신의 cmd와 같은 자바 라이브러리/프레임 워크를 사용해 보려고합니다. 유닉스가 권력을 장악하는 것을 좋아하지만, 작성한 코드는 그다지 강력하지도 않고 휴대하기도 쉽지 않습니다. –