gpt4 book ai didi

java - 如何在 Windows 上从命令行运行 Java 程序?

转载 作者:IT老高 更新时间:2023-10-28 12:16:35 26 4
gpt4 key购买 nike

我正在尝试从 Windows 的命令行执行 Java 程序。这是我的代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile
{
public static void main(String[] args)
{

InputStream inStream = null;
OutputStream outStream = null;

try
{

File afile = new File("input.txt");
File bfile = new File("inputCopy.txt");

inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);

byte[] buffer = new byte[1024];

int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0)
{

outStream.write(buffer, 0, length);

}

inStream.close();
outStream.close();

System.out.println("File is copied successful!");

}
catch (IOException e)
{
e.printStackTrace();
}
}
}

我不确定如何执行该程序 - 有什么帮助吗?这在 Windows 上可行吗?为什么它与其他环境不同(我以为 JVM 是一次编写,随处运行)?

最佳答案

来源:javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

关于java - 如何在 Windows 上从命令行运行 Java 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com