Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

java - 如何在Windows上从命令行运行Java程序?(How do I run a Java program from the command line on Windows?)

I'm trying to execute a Java program from the command line in Windows.

(我正在尝试从Windows中的命令行执行Java程序。)

Here is my code:

(这是我的代码:)

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();
        }
    }
}

I'm not sure how to execute the program - any help?

(我不确定如何执行程序-有帮助吗?)

Is this possible on Windows?

(在Windows上可以吗?)

Why is it different than another environment (I thought JVM was write once, run anywhere)?

(为什么它不同于另一个环境(我以为JVM只写一次,可以在任何地方运行)?)

  ask by Elizabeth Turner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Source: javaindos.

(资料来源: javaindos。)

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

(假设您的文件位于C:\ mywork \)

Run Command Prompt

(运行命令提示符)

 C:\> cd \mywork 

This makes C:\mywork the current directory.

(这使C:\ mywork成为当前目录。)

 C:\mywork> dir 

This displays the directory contents.

(这将显示目录内容。)

You should see filenamehere.java among the files.

(您应该在文件中看到filenamehere.java。)

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

This tells the system where to find JDK programs.

(这告诉系统在哪里可以找到JDK程序。)

 C:\mywork> javac filenamehere.java 

This runs javac.exe, the compiler.

(这将运行编译器javac.exe。)

You should see nothing but the next system prompt...

(除了下一个系统提示,您什么都看不到。)

 C:\mywork> dir 

javac has created the filenamehere.class file.

(javac已创建filenamename.class文件。)

You should see filenamehere.java and filenamehere.class among the files.

(您应该在文件中看到filenamehere.java和filenamehere.class。)

 C:\mywork> java filenamehere 

This runs the Java interpreter.

(这将运行Java解释器。)

You should then see your program output.

(然后,您应该看到程序输出。)

If the system cannot find javac, check the set path command.

(如果系统找不到javac,请检查set path命令。)

If javac runs but you get errors, check your Java text.

(如果javac运行但出现错误,请检查Java文本。)

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 HelloWorld命令的拼写和大小写。)

Java is case-sensitive!

(Java区分大小写!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...