使用Windows cmd中的参数启动java应用程序?
对不起,我的英语说得不太好 当我想从windows cmd启动java项目时,我会:使用Windows cmd中的参数启动java应用程序?,java,windows,parameters,cmd,Java,Windows,Parameters,Cmd,对不起,我的英语说得不太好 当我想从windows cmd启动java项目时,我会: javac main.java(main=我的文件名) 然后:javamain 但是如果我使用参数,我该怎么做呢 我试过:java主参数,但效果不好 你有主意吗 非常感谢您的帮助:) 这里有代码: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.I
javac main.java
(main=我的文件名)
然后:javamain
但是如果我使用参数,我该怎么做呢
我试过:java主参数
,但效果不好
你有主意吗
非常感谢您的帮助:)
这里有代码:
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行:
stringnamefile=parameters[0]代码>所以看起来您可以读取参数,但是您得到的是FileNotFoundException。按照读取文件的方式,exercie4.txt应该存在于src文件夹中,否则当前代码将无法读取它。传递文件的完整路径或更新代码好吧,代码看起来是正确的,但可以肯定的是它不会工作,因为文件不在运行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();
}
您应该能够通过引用args[]数组来读取命令行参数。你能分享你试图运行的代码和确切的命令吗?非常感谢!!现在一切都好了:)我把我的文件放在src文件夹中:)很高兴我能帮上忙:)如果有机会,请接受答案