![]() ![]() Without recompiling, it runs as expected. Run your JAR file with the java command: $ java -jar Guess.jarĬopy your JAR file from Linux to a macOS or Windows computer and try running it. You can view the contents of the JAR file: $ jar tvf Guess.jarĠ Wed Nov 25 10:33:10 NZDT 2020 META-INF/ĩ6 Wed Nov 25 10:33:10 NZDT 2020 META-INF/MANIFEST.MFġ572 Wed Nov 25 09:42:08 NZDT 2020 com/example/guess/Main.classĪnd you can even extract it with the xvf options. ![]() Its main class is defined as an extension of the package name, and the class is com/example/guess/Main.class. The jar command is a lot like the tar command, so many of the options may look familiar: $ jar cfme Guess.jar \įrom the syntax of the command, you may surmise that it creates a new JAR file called Guess.jar with its required manifest data located in Manifest.txt. You're all set to package your application into a JAR (Java archive). This produces a file called Main.class in com/example/guess: $ javac src/Guess.java -d com/example/guess Next, compile your game into a Java class. ("number is " String.valueOf(NUMBER)) //DEBUG Private static final int NUMBER = r.nextInt(100) 1 Private static final Random r = new Random() It's a great practical experiment for learning a new programming language. This "guess the number" program exercises several concepts in programming languages: how to assign values to variables, how to write statements, and how to perform conditional evaluation and loops. ![]() If you don't have it, check out these links to install Java on Linux, macOS, or Windows.Īfter installing it, run this Java command in a terminal to confirm the version you installed: $ java -version Guess the number To do this exercise, you must have Java installed. In this article, I demonstrate how to create a simple guessing game in Java. I've found that simple games are both fun to write and useful in exploring a language's abilities.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |