Javaでのプログラム動作についての質問

このQ&Aのポイント
  • 卒業研究でJavaを扱っているが、思った動作をするプログラムが作れない。
  • Windowsのパソコンで実行すると、コマンドプロセッサでdirを実行し、その後ファイルにする。
  • Javaのプログラムが正しく動作しない理由がわからない。
回答を見る
  • ベストアンサー

java わからない。

今、卒業研究でJAVAを扱っています。 しかし、思った動作をするプログラムが作れません。 時間もなくなってきたので、okwaveに投稿しました。 お願いします。 パソコンはwindowsです。 プログラムを実行すると, (1)コマンドプロセッサでdirを実行してくれる。 (2)実行した後にファイルにする。 実行するときは[dir > dir.txt]のようにしたいです。 今まで書いてきた例をいかに描きます。 import java.io.*; public class TestExec1 { public static void main(String[] args) { try { String s1 = "dir"; Process process = Runtime.getRuntime().exec(s1); InputStream is = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } どこが間違っているかわかりません。 よろしくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • buriburi3
  • ベストアンサー率44% (353/792)
回答No.1

Windowsパソコンにdirの実行モジュール(dir.exe/dir.com)は存在しません。 存在しないので実行できません。 dirはコマンドの組み込みコマンドなので NT系なら String s1 = "cmd /C\"dir\" > dir.txt";

fifth_ave
質問者

お礼

vist ってNT系ですか?

その他の回答 (1)

  • buriburi3
  • ベストアンサー率44% (353/792)
回答No.2

VistaはWindowsNT 6.0です。

fifth_ave
質問者

お礼

ありがとうございます。

関連するQ&A

  • dirコマンドを実行して、結果を表示するJavaプログラム(プログラム

    dirコマンドを実行して、結果を表示するJavaプログラム(プログラムは一番下に)なのですが このプログラムをfor文でfor(int i=0;i<=13;i++)とこのように やったら、以下のような結果になったのですが //実行結果 ドライブ C のボリューム ラベルがありません。 ボリューム シリアル番号は xxxx-xxxx です C:\Users\z\zw\zww(1) のディレクトリ 2010/10/19 11:11 <DIR> . 2010/10/19 11:11 <DIR> .. 2010/10/19 11:11 301 .classpath 2010/10/19 11:11 381 .project 2010/10/19 11:11 <DIR> .settings 2010/10/19 11:16 <DIR> bin 2010/10/19 11:16 <DIR> src 2 個のファイル 682 バイト 5 個のディレクトリ 48,884,387,840 バイトの空き領域 //実行結果ここまで これをwhileで while(true){ if(a.getLine()==null){ break; } System.out.println(a.getLine()); } というようにやると以下のような実行結果になって、for文と同じ結果になるのが正 しい結果だと思うのですがなりませんでした。ループを抜けるために、break文をや ったのですが、自分が考えた条件は、getLine()メソッドは読み込む行が無くなった ら、nullを返すので、a.getLineがnullと同じになったらbreakをするという風に自 分ではしたつもりなのですが、条件が違うのかfor文のような結果が出ませんでした。 whileの場合なぜこのような結果になってしまったのかわからなく、 手詰まりになってしまったので、どなたかご教授ください 実行結果 ボリューム シリアル番号は xxxx-xxxx です C:\Users\z\zw\zww(1) のディレクトリ 2010/10/19 11:11 <DIR> . 2010/10/19 11:11 301 .classpath 2010/10/19 11:11 <DIR> .settings 2010/10/19 11:16 <DIR> src 5 個のディレクトリ 48,886,591,488 バイトの空き領域 実行結果ここまで //プログラム import java.io.*; class DirCmd{ BufferedReader br; DirCmd(){//コンストラクタの定義(dirコマンドをここで実行) try{ Process p = Runtime.getRuntime().exec("cmd /c dir"); InputStream is = p.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); }catch(IOException e){} } public String getLine(){//dirコマンドの実行結果の一行を読む String line = null;// 読み込む行が無くなったらnullを返す try{ line = br.readLine(); }catch(IOException e){} return(line); } public static void main(String [] args){ DirCmd a = new DirCmd(); while(true){ if(a.getLine()== null){ break; } System.out.println(a.getLine());     } } }

    • ベストアンサー
    • Java
  • javaからのFFMPEGの利用

    現在、windows vista に ffmpeg をインストールし、java(1.6)から以下のようにffmpeg.exeを呼び出しています。 -------------------------------------------------------------- public static void main(String[] args) { ProcessBuilder builder = new ProcessBuilder("C:/Tools/3GP_Converter034/cores/debug.bat"); try { Process process = builder.start(); InputStream is = process.getInputStream(); //標準出力 printInputStream(is); InputStream es = process.getErrorStream(); //標準エラー printInputStream(es); } catch (Exception e) { throw new ConversionException(e); } } public static void printInputStream(InputStream is) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(is)); for (;;) { String line = br.readLine(); if (line == null) break; System.out.println(line); } } -------------------------------------------------------------- debug.batの中身は以下です。 -------------------------------------------------------------- C:/Tools/3GP_Converter034/cores/ffmpeg.exe -y -i c:/work/a.mpg -f flv -vcodec flv -r 25 -b 900k -ar 44100 -ab 64k c:/work/out99.flv -------------------------------------------------------------- debug.batを直接呼び出すと正常にout99.flvが出力されるのですが、java経由だとうまくできません。 printInputStreamメソッドから以下のsysytem out文が出力されたまま止まってしまいます。 C:\java>C:/Tools/3GP_Converter034/cores/ffmpeg.exe -y -i c:/work/a.mpg -f flv -vcodec flv -r 25 -b 900k -ar 44100 -ab 64k c:/work/out99.flv まったく理由がわかりませn。 どなたかご教示ください;;

  • javaで大きなファイルを解析できますか?

    100MBくらいのアクセスログをjavaを使って解析しようと思うのですが、どうしても、エラーが出ます。 読み込み方が間違っているのでしょうか? まったくわかりません。 教えていただけないでしょうか? また、これを知ったきっかけも教えていただけましたら幸いです。 宜しくお願い致します。 読み込みの部分↓↓↓ class kaiseki { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("log.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is));

    • ベストアンサー
    • Java
  • 403を返されてしまいます。

    グーグルの検索結果を取得したいのですが 以下のようなプログラムでは403を返されてしまいます。 他のページではちゃんと取得できるのですが 原因は何なんでしょうか。また、これを回避する方法はあるのでしょうか? import java.net.*; import java.io.*; public class test { public static void main(String args[]) { try { URL objURL = new URL("http://www.google.co.jp/search?hl=ja&q=java&lr="); URLConnection objURLCnn = objURL.openConnection(); InputStream inputStream = objURLCnn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "JISAutoDetect")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.co.jp/search?hl=ja&q=java&lr= at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at test.main(test.java:10)

  • javaについて

    こういうプログラムを組んだんですが、うまく実行できません。 どんな改善をしたらよいでしょうか? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Sample { public static void main(String[] args) { String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=java", "UTF-8"); htmlSrc = htmlSrc.replaceAll("<.+?>| ", ""); htmlSrc = htmlSrc.replaceAll(".*件-", ""); htmlSrc = htmlSrc.replaceAll("秒.*", "秒"); System.out.println(htmlSrc); } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } } 以下のエラーが表示されるんですが、どうしたらよいでしょうか?? 環境が悪いのでしょうか?? java.net.ConnectException: Operation timed out

    • ベストアンサー
    • Java
  • プログラムからの返答の意味がわかりません..

    import java.io.*; public class Teme { public static void main(String [] args) { String [] command = { "C:\\Users\\yuzumodoki\\Desktop\\chunkparser-1.0\\chunkparser-1.0\\parser.exe", "aaaa" }; // 外部プログラムを実行 Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec(command); } catch(Exception exception) { System.err.println(exception.getMessage()); exception.printStackTrace(); System.exit(-1); } // 外部プログラムの標準出力から読み込み BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); System.out.println(br); try { String line; while((line = br.readLine()) != null) { System.out.println(line); } } catch(Exception exception) { System.err.println(exception.getMessage()); exception.printStackTrace(); System.exit(-1); } finally { try { br.close(); } catch(Exception exception) { System.err.println(exception.getMessage()); exception.printStackTrace(); System.exit(-1); } } } } 上記のプログラムはparser.exeにアクセスし,そのアプリケーションに文字列を送り,処理をしてその結果を取得するというものです. 本来"aaaa"と送ると"(aaaa aaaa)"と返ってくるプログラムなのですが, java.io.BufferedReader@89ae9e と返ってきます.これはなぜだかわかる方いらっしゃいますか?また,aaaa aaaaと本来の値を返すにはどうすればいいかわかる方いらっしゃいませんか.. もしわかる方いらっしゃたら教えて頂けたらすごくうれしいです(つj)

  • Javaで外部ファイルの実行

    Javaで外部ファイルの形態素解析ツールのjumanを実行させようとしてるのですが上手くいきません。 ソースは以下のようにしています。 public class CommandExec { /** * 外部コマンドを実行します。 * またリタンーン値で、標準出力、エラー出力 、リターンコードを取得します。 * 例: * execCommand("notepad.exe"); * * @see execCommand(String[] cmds) * ※実行するコマンドに引数(パラメータ)がある場合は、 * 以下を使用してください。 * @param cmd 実行するコマンド * @return コマンド実行結果情報を保持するString配列 * 配列[0] ⇒ 標準出力 * 配列[1] ⇒ エラー出力 * 配列[2] ⇒ リターンコード * @throws IOException 入出力エラーが発生した場合 */ String b; public static String[] execCommand(String cmd) throws IOException, InterruptedException { return execCommand(new String[] { cmd }); } /** * 外部コマンドを引数(パラメータ)を指定して実行します。 * またリタンーン値で、標準出力、エラー出力 、リターンコードを取得します。 * 例: * execCommand(new String[]{"notepad.exe","C:\test.txt"}); * * Process.waitFor()を実行していますので、外部コマンドの実行が * 終了するまでこのメソッドは待機します。 * * @see execCommand(String cmd) * ※実行するコマンドに引数がない場合は簡易的にこちらを * 使用してください。 * @param cmds 実行するコマンドと引数を含む配列 * @return コマンド実行結果情報を保持するString配列 * 配列[0] ⇒ 標準出力 * 配列[1] ⇒ エラー出力 * 配列[2] ⇒ リターンコード * @throws IOException 入出力エラーが発生した場合 */ public static String[] execCommand(String[] cmds) throws IOException, InterruptedException { String[] returns = new String[3]; String LINE_SEPA = System.getProperty("line.separator"); Runtime r = Runtime.getRuntime(); Process p = r.exec(cmds); InputStream in = null; BufferedReader br = null; try { in = p.getInputStream(); StringBuffer out = new StringBuffer(); br = new BufferedReader(new InputStreamReader(in,"sjis")); String line; while ((line = br.readLine()) != null) { out.append(line + LINE_SEPA); System.out.println(line); } System.out.print("\n"); returns[0] = out.toString(); br.close(); in.close(); in = p.getErrorStream(); StringBuffer err = new StringBuffer(); br = new BufferedReader(new InputStreamReader(in)); while ((line = br.readLine()) != null) { err.append(line + LINE_SEPA); } returns[1] = err.toString(); returns[2] = Integer.toString(p.waitFor()); return returns; } finally { if (br != null) { br.close(); } if (in != null) { in.close(); } } } public static void main(String[] args) throws IOException, InterruptedException { ArrayList a = new ArrayList(); CommandExec.execCommand(new String[]{"C:\\Program Files\\juman\\juman.exe","<","C:\\Program Files\\juman\\test1.txt"}); } } これをおなじ解析ツールのchasenを実行した場合は上手くいくのですがなぜでしょうか? chasenの場合は以下のようにしてます。 CommandExec.execCommand(new String[]{"C:\\Program Files\\ChaSen\\chasen.exe","C:\\Program Files\\Chasen\\test1.txt"}); よろしくお願いします。

    • ベストアンサー
    • Java
  • Runtime.exec()について

    Runtime.exec()について質問させて下さい。 現在、Windowsのサーブレット上のファイル操作で ファイルの属性ごと(読み取り専用)コピーしたいので Runtime.exec()からxcopyコマンドを呼び出してコピーしています。 (JavaのAPIでは属性ごとコピー、属性変更ができない!?ため) エディタはeclipse3.5を使用しております。 eclipse上のTomcat6.0では綺麗にコピーしてくれるのですが、 Tomcat6.0のみでの実行ではコピーできません(0個のファイルをコピー) eclipse3.5上のTomcat設定とTomcat自体のJVM設定でのJREパスは同じJRE を参照しています。常にEclipseを起動させた状態!!ってのは厳しいので なんとかTomcatのみでコピーを成功させたいので助言お願いいたします。 下記ソースで 4個の読み取り属性を強制上書きコピーしますと、表示は Eclipse3.5上では → 「4個のファイルコピーに成功」表示 Tomcat6上では   → 「0個のファイルコピーに成功」表示 となります。。。 【以下ソース】 CopyFrom = "コピー元パス"; CopyTo  = "コピー先パス"; try{  String[] cmd = new String[]{"cmd","/c","xcopy",CopyFrom+"*.*",CopyTo,"/y","/r","/k","/h"};  Runtime Run = Runtime.getRuntime();  Process process = Run.exec(cmd);  //実行結果取得  InputStream is = process.getInputStream();  BufferedReader br = new BufferedReader(new InputStreamReader(is));  String Line = null;  while((Line = br.readLine()) != null){   System.out.println(Line);  } }catch(IOException e){  System.out.println(e); } 【環境】 ・Windows Server2003 ・Java1.6.0_13(パスは通しています) ・Tomcat6.0.14

    • ベストアンサー
    • Java
  • process has not exited

    実行可能なjarファイルをコマンド実行しその戻り値を取得したくて下記プログラムを書きました。 ところがprocess.exitValue() のところで  process has not exited というエラーを起こしてしまいます。原因はお分かりになりますでしょうか。 String command = "java -jar test.jar test; Process process = Runtime.getRuntime().exec(command); System.out.println("▼▼▼実行中のコマンド=" + command); //int no = process.waitFor(); //exec()が非同期実行であるため実行結果が返ってくるまで待つ System.out.println("▼▼▼コマンドの実行完了"); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while ( (line = in.readLine())!=null ) { line = in.readLine(); System.out.println(line); } System.out.println("★★★コマンドの戻り値=" + process.exitValue()); if(process.exitValue() != 0) { process.destroy(); } in.close(); } } catch(Exception e) { if( process != null ) { process.destroy(); } e.printStackTrace(); }

    • ベストアンサー
    • Java
  • Javaで外部コマンドの実行した結果の取得

    Javaで外部コマンドを実行してその結果を取得しようとしてEclipse上で 下記サンプルを作成しました。ここで不思議なことが起きました。 下記を実行すると System.out.println(in.ready()) の部分でfalseが返り処理がwhileループの中に入らない状態です。 しかし、System.out.println(in.ready())の部分にブレークポイントを付けて1行ずつ実行していくと trueが返りwhileループの中の結果を読み取る処理にたどり着きます。 これはどういうことでしょうか。 public class Test { public static void main(String[] args) { String command = "hostname"; try { Process process = Runtime.getRuntime().exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); System.out.println(in.ready());             while(in.ready()) { } in.close(); } catch(Exception e) { e.printStackTrace(); } } }

    • ベストアンサー
    • Java

専門家に質問してみよう