如何从Windows上的命令行运行Java程序? [英] How do I run a Java program from the command line on Windows?

查看:823
本文介绍了如何从Windows上的命令行运行Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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;

公共类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;
//以字节
复制文件内容while((length = inStream.read(buffer))> 0)
{

outStream.write(buffer ,0,长度);

}

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

System.out.println(文件复制成功!);

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

我不知道怎么样执行程序 - 任何帮助?这可能在Windows上吗?为什么它与另一个环境不同(我认为JVM只写一次,在任何地方运行)?

解决方案

来源: javaindos。


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



运行命令提示符

  C:\> cd \ mywork 

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

  C:\ mywork> dir 

显示目录内容。您应该在文件中看到
filenamehere.java。

  C:\ mywork> set path =%path%; C:\Program Files \ Java @\\\\\\\ b 

这告诉系统在哪里可以找到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
HelloWorld命令中的拼写和
大小写。 Java区分大小写!



I'm trying to execute a Java program from the command line in Windows. 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? Why is it different than another environment (I thought JVM was write once, run anywhere)?

解决方案

Source: 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!

这篇关于如何从Windows上的命令行运行Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆