Java - 如何在Windows中运行控制台程序

时间:2021-10-01 00:18:31

Hi I wrote wordquiz program on Java. Using Eclipse in Unix. In my linux machine it works fine. Here is a source code https://github.com/HighlanderGe/Words So, only basic package is used. In windows compilation of such code as jar doesn't run. Neither in Mac. As I guess problem is that in linux it is made to run from console, and console is comething very native for linux, but in Windows and I think in Mac too, cmd should be called, and from there it somehow to run.. but I bet cmd has no idea what is java. So some java console is needed for it?

嗨我在Java上写了wordquiz程序。在Unix中使用Eclipse。在我的linux机器上它工作正常。这是一个源代码https://github.com/HighlanderGe/Words因此,只使用基本包。在windows中编译像jar这样的代码不运行。在Mac中都没有。我猜想问题是在Linux中它是从控制台运行的,而控制台对于Linux来说是非常原生的,但在Windows中我认为在Mac中也应该调用cmd,并且从那里以某种方式运行..但是我敢打赌cmd不知道什么是java。所以需要一些java控制台吗?

2 个解决方案

#1


1  

The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.

问题不在于Mac或Windows,问题在于您没有在Eclipse中以相同的方式在不同的计算机上设置工作区。

You can build your program on the command line in all environments in the same way. You just have to know the right steps.

您可以以相同的方式在所有环境中的命令行上构建程序。你只需知道正确的步骤。

First of all, there's an error in your code on line 25 in WordDatabase. Instead of:

首先,WordDatabase中第25行的代码中存在错误。代替:

dictionary = new ArrayList<>();

it should be:

它应该是:

dictionary = new ArrayList<String>();

After that, you can build your code like this:

之后,您可以像这样构建代码:

javac -d . *.java

And run it like this:

并运行它:

java wordquizz/Wordquizz

This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.

这应该适用于任何具有Java的系统,您只需要弄清楚如何在Eclipse中以相同的方式在不同的计算机上设置工作区。

UPDATE

UPDATE

I forked and converted your project to a Maven project:

我将您的项目分叉并转换为Maven项目:

https://github.com/janosgyerik/*-Words

https://github.com/janosgyerik/*-Words

After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.

将其克隆到PC后,可以使用File |导入Eclipse导入...菜单,然后是现有的Maven项目选项。它应该适用于所有操作系统。

Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:

Maven是用于构建Java项目的推荐工具,这是一件好事。安装maven后,可以使用以下命令构建项目:

mvn compile

You can package the project into a jar file with:

您可以将项目打包到一个jar文件中:

mvn package

You can run your code with either of these commands:

您可以使用以下任一命令运行代码:

# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz

# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz 

If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.

如果您喜欢这些改进,请尽快合并我的回购。我不会永远保留它,我会在某个时候删除它。

UPDATE 2

更新2

To make a jar executable, you need to add inside a manifest file like this:

要使jar可执行,您需要在清单文件中添加如下:

Main-Class: wordquizz.Wordquizz

You create the jar file with a command like this:

使用如下命令创建jar文件:

jar cvfm package.jar manifest.txt wordquizz/*.class

I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.

我更新了我的GitHub存储库,所以现在如果你运行mvn包,它会自动添加正确的清单,生成的jar文件将是可执行的。

#2


0  

If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:

如果您在Windows下安装了Java,则必须调整Path变量,因此cmd知道java可执行文件的位置。有关如何设置它的好教程,您可以在这里找到:

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.

之后,您可以简单地转到源代码所在的目录,并使用与linux下相同的命令来编译和运行您的应用程序。

#1


1  

The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.

问题不在于Mac或Windows,问题在于您没有在Eclipse中以相同的方式在不同的计算机上设置工作区。

You can build your program on the command line in all environments in the same way. You just have to know the right steps.

您可以以相同的方式在所有环境中的命令行上构建程序。你只需知道正确的步骤。

First of all, there's an error in your code on line 25 in WordDatabase. Instead of:

首先,WordDatabase中第25行的代码中存在错误。代替:

dictionary = new ArrayList<>();

it should be:

它应该是:

dictionary = new ArrayList<String>();

After that, you can build your code like this:

之后,您可以像这样构建代码:

javac -d . *.java

And run it like this:

并运行它:

java wordquizz/Wordquizz

This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.

这应该适用于任何具有Java的系统,您只需要弄清楚如何在Eclipse中以相同的方式在不同的计算机上设置工作区。

UPDATE

UPDATE

I forked and converted your project to a Maven project:

我将您的项目分叉并转换为Maven项目:

https://github.com/janosgyerik/*-Words

https://github.com/janosgyerik/*-Words

After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.

将其克隆到PC后,可以使用File |导入Eclipse导入...菜单,然后是现有的Maven项目选项。它应该适用于所有操作系统。

Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:

Maven是用于构建Java项目的推荐工具,这是一件好事。安装maven后,可以使用以下命令构建项目:

mvn compile

You can package the project into a jar file with:

您可以将项目打包到一个jar文件中:

mvn package

You can run your code with either of these commands:

您可以使用以下任一命令运行代码:

# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz

# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz 

If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.

如果您喜欢这些改进,请尽快合并我的回购。我不会永远保留它,我会在某个时候删除它。

UPDATE 2

更新2

To make a jar executable, you need to add inside a manifest file like this:

要使jar可执行,您需要在清单文件中添加如下:

Main-Class: wordquizz.Wordquizz

You create the jar file with a command like this:

使用如下命令创建jar文件:

jar cvfm package.jar manifest.txt wordquizz/*.class

I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.

我更新了我的GitHub存储库,所以现在如果你运行mvn包,它会自动添加正确的清单,生成的jar文件将是可执行的。

#2


0  

If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:

如果您在Windows下安装了Java,则必须调整Path变量,因此cmd知道java可执行文件的位置。有关如何设置它的好教程,您可以在这里找到:

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.

之后,您可以简单地转到源代码所在的目录,并使用与linux下相同的命令来编译和运行您的应用程序。