简体   繁体   中英

Java - how to run console program in windows

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?

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.

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:

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.

UPDATE

I forked and converted your project to a Maven project:

https://github.com/janosgyerik/StackOverflow-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.

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:

mvn compile

You can package the project into a jar file with:

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

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

Main-Class: wordquizz.Wordquizz

You create the jar file with a command like this:

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM