我正在尝试从 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是一次编写,随处运行)?
Source: 爪哇语。
假设您的文件位于 C:\mywork\
运行命令提示符
C:\> cd \mywork
这使得 C:\mywork 成为当前目录。
C:\mywork> dir
这将显示目录内容。你应该看到
文件中的 filenamehere.java 。
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
这告诉系统在哪里可以找到 JDK 程序。
C:\mywork> javac filenamehere.java
这将运行编译器 javac.exe。除了
接下来系统提示...
C:\mywork> dir
javac 已创建 filenamehere.class 文件。你应该看到
文件中的 filenamehere.java 和 filenamehere.class 。
C:\mywork> java filenamehere
这将运行 Java 解释器。然后你应该看到你的程序
输出。
如果系统找不到javac,请检查set path 命令。如果javac
运行但出现错误,请检查 Java 文本。如果程序
编译但出现异常,请检查拼写并
文件名、类名和java中的大写
你好世界命令。 Java 是区分大小写的!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)