2015-10-26 39 views
0

我很抱歉,但我不会说英语非常好.. :)使用Windows cmd中的参数启动java应用程序?

当我想推出从Windows Java项目在cmd我做的:

javac main.java(主=我的文件名)

那么:java main

但是如果我使用参数,我该怎么办?

我试过了:java main parameters但它不好。

你有什么想法吗?

非常感谢您的帮助:)

Image

这里有代码:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import elements.*; 

public class Main 
{ 
    public static void main(String[] parameters) 
    { 
     try 
     { 
      String nameFile = parameters[0]; 
      FileInputStream fileInputStream = new FileInputStream(new File(nameFile)); 
      Processing processing = new Processing(); 
      processing.read(fileInputStream); 
      try 
      { 
       fileInputStream.close(); 
      } 
      catch (IOException exception) 
      { 
       System.out.println("Une erreur s'est produite lors de la fermeture de l'InputStream"); 
      } 
     } 
     catch(FileNotFoundException exeption) 
     { 
      System.out.println("Le nom de fichier placé en paramètre est incorrect"); 
     } 
    } 
} 

的问题是该行14:String nameFile = parameters[0];

+0

您应该能够通过引用args []数组来读取命令行参数。你能分享你试图运行的代码和确切的命令吗? – AbtPst

+0

我编辑了我的帖子:) –

回答

0

所以看起来像你能够读取参数,但你得到一个FileNotFoundException。您正在阅读文件的方式中,exercice4.txt应该出现在您的src文件夹中,否则您当前的代码将无法读取它。或者通过文件的完整路径或更新代码

+0

非常感谢!现在一切都很好:) 我把我的文件放在src文件夹中:) –

+0

很高兴我可以帮助:)请接受答案,当你有机会 – AbtPst

0

那么,代码似乎是正确的,但肯定它不会工作,因为该文件不在您运行java blah blah命令的目录中。尝试运行

java Main ..\exercice4.txt 

顺便说一句,这类问题通常很容易用几个打印语句来调试,如:

String nameFile = parameters[0]; 
System.out.println("Trying to open file "+nameFile); 
FileInputStream fileInputStream = new FileInputStream(new File(nameFile)); 

此外,当有一个例外是用来显示异常有用堆栈跟踪,例如:

catch(FileNotFoundException exeption) 
{ 
    System.out.println("Le nom de fichier placé en paramètre est incorrect"); 
    exeption.printStackTrace(); 
} 
相关问题