Search code examples
javacmdcompilationcommand-promptjavac

How do I run a java program through the console (command prompt)?


I would like to test out this game by running it in my command prompt. I am able to compile both of the java files alone but not sure what to do from there. So far I've:

  • Installed JRE and JDK
  • Added the directory of my bin folder (C:\Program Files\Java\jdk-13.0.1\bin) to the "Path" Environment Variables (both User Variables and System Variables)
  • Copied and pasted the Snake.java and Board.java into a folder called "javaprogram" on a separate drive (D:)

So far my inputs in cmd are:

  1. D:
  2. cd javaprogram
  3. javac Snake.java Board.java

What do I do from here?

This is the link to the source code of the 2 java files. http://zetcode.com/tutorials/javagamestutorial/snake/


Solution

  • Put all you files in the directory javaprogram

    I mean all of them : Snake.java,Board.java,head.png,dot.png and apple.png

    Then you edit files Snake.java and Board.java

    In Snake.java, remove package declaration at top of the file.

    In Board.java, remove package declaration at top of file as well as edit the path of image files.

    eg. ImageIcon iid = new ImageIcon("dot.png");

    Once you have done all the above mentioned changes, use the below mentioned command:

    javac *.java - to compile files

    javaw Snake - to execute the program

    Check whats the difference between java and javaw and javac.

    If you are sending this game to someone just pack it in executable jar file.

    Give my answer green tick :P