How to run a Java console program? (and also so that everything is in Russian) - Basics of coding. How to run a java application from the command line

One of the favorite questions of beginning programmers in Java (and not only in Java)
Let's start with compiling and running.
Firstly, Java applications cannot do without a virtual machine; for you to have one, roughly speaking, you need to install the JDK (Java Development Kit), you can download it directly from the Oracle website.
Let's look at the environment variables that we have.
This is done like this: right-click on “My Computer” -> “Properties” -> “Advanced” -> “Environment Variables”.
The PATH variable must contain the path to the JDK executable files, where java.exe(a virtual machine that executes byte code compiled by the javac.exe utility) and javac.exe(compiler, translates files with the java extension into byte code, files with the class extension)
So let's start by organizing the folders.
For example, for a console game about a grandmother, we create a Game folder.
Next we need to create a folder in it classes And src.
IN src we will store files java.
IN classes compiled class files.

Let's use the example from the article.
In folder src create a folder babgame.
Each package package- this is a folder, and babgame We have a package, don't forget.
It is recommended to name packages after domain names, because The package name is unique. For example, if we have a package su.clan.pr0gram, then you will need to create three subfolders, first su, then clan and at the end pr0gram, and it already contains the files of our program.
We create a file with the java extension in babgame and write the program code there, save it, exit babgame, then exit src, get into Game.
Writing a script for the console - compile.cmd

Javac -classpath ./classes -d ./classes -s ./src src/babgame/Main.java -encoding Cp1251
pause


pause can be removed, this is a delay in case the compiler generates errors.
javac- as you probably already understood, this is a compiler, it accepts a couple of dozen parameters, a couple of which we used.
-classpath ./classes- indicate in which folder we have classes
-d ./classes- indicate in which folder the class files will be generated
-s ./src- indicate the folder with the sources (in principle, you don’t have to write)
src/babgame/Main.java- the file itself is in the src folder (for me it’s called Main.java)
By the way, don’t forget, the name of the class described in a file with the java extension must match the name of the file (without extension) (in some systems even the case of writing, lower or upper, depends)
-encoding Cp1251- indicates in which encoding the file with the source code of the program should be read. (it is assumed that the source is written for Windows)

The compiled file will be placed in the folder classes.
We write the following script:
run.cmd

Java -Dfile.encoding=Cp866 -classpath ./classes Main


java- runs our class file.
-Dfile.encoding=Cp866- indicates in which encoding the output should be produced.
Since our console outputs to Cp866 by default, we’ll do that.
-classpath ./classes- indicate the folder where we store classes.
Main- the name of the class where we have the main() method, without which, as we know, the program will not start.

We create these two scripts in the directory Game, let me remind you again.
Well, that's all, you can play until the morning!

How to run a java program?

Master's answer:

An application written in the java language is a jar archive or a set of *.java and *.class files, if the components of this program are not compiled into an executable jar archive. In order to run such an application, you will need to install the Java Runtime Environment program.

To do this, you need to go to the page of the official website of java developers, which is located at http://java.sun.com/javaee/sdk/, and then download the latest version of the java machine. Next, you will need to install the program on the hard drive of your personal computer and indicate to the OS the path to the files you installed, for which you need to add the path to the location where the program is installed to the path parameter of the environment variables (the tab is called “Advanced”, located in the computer properties window).

Using the main menu or entering the cmd command in the “Run” field, you need to launch the command line. On the command line, go to the directory of the java program you want to run. After completing this procedure, enter the command java -cp [file name].jar [name of the main class of the program] and press the Enter key, this will start executing the file.

In order to compile java files, you need to enter the command javac[main file name].java, followed by java -cp [file name]. Having received information about the parameters for launching java files from the command line, you need to enter the java -help statement and press the Enter key.

The screen will display a complete list of available commands along with their options. All commands must be entered accurately, because entering the wrong combination can damage the entire operating system of your personal computer.

If you want to run mini-games on your computer that are written in java for mobile devices, then you will need to download the MidpX program. This application will allow you to run jar files without unnecessary complications. After you install this software, right-click on one of the jar files and select the item called “Properties”. In the window that appears, you will need to click on the “Change” button and select the program suitable for opening this type of file, that is, the MidpX utility.

Java Web Start software allows you to download and run Java applications from the Internet. Java Web Start Software:
  • Allows you to activate applications with one click
  • Ensures you are using the latest versions of applications
  • Eliminates the need for complex installation and upgrade procedures

How to get the Java Web Start software:

Java Web Start has been included with the Java Runtime Environment (JRE) since the release of Java 5.0. This means that when you install Java, Java Web Start is automatically installed. When you first load a Java application that supports Java Web Start technology, the Java Web Start software starts automatically. Java Web Start places the downloaded application entirely in your computer's local cache. This way, the application will launch almost instantly, since all the resources needed for it are available on the local computer. Each time you launch an application, Java Web Start checks for a new version on the site, and if there is one, it automatically downloads and runs it.

Running an application using Java Web Start

Using a browser
Using the desktop icon

If you use an application frequently, you can create a shortcut on your desktop or Start menu. Java Web Start may prompt you to create shortcuts or entries in the Start menu. If "Yes" is selected, all subsequent launches of the application can be performed without a browser.

Using the Java Application Cache Viewer

Java Web Start also provides an Application Cache Viewer that you can launch from the Java Control Panel. The Application Cache Viewer allows you to directly launch downloaded applications.

Instructions for launching using the Application Cache Viewer
  1. Open menu Start > Settings > Control Panel> double click on the icon Java. The Java Control Panel will appear on the screen.
  2. Go to the tab General
  3. Click the button View In chapter Temporary Internet Files
  4. Select the application you want to launch from the list and double-click on its name
Using the command line

To run the application using the command line, enter the javaws command jnlp_url, Where jnlp_url- URL of the application's jnlp file.

The Java programming language is rapidly gaining popularity due to its simplicity and functionality. Many mobile and computer applications are written in this language. It is worth noting that applications for Android devices are written in Java.

Download Java is available on the official website completely for free. However, there may be a problem with the program version. First of all, you have to choose between 32-bit And 64-bit version. If the computer allows it, then of course it’s better to stop at 64, it’s not so strict restrictions on memory usage.

Then you need to decide - install Java Development Kit (JDK) or Java Runtime Environment (JRE). Everything is extremely simple and clear if you translate the abbreviation. JDK is a development kit. It is used to create programs in this language.

JRE It only allows you to launch applications. By the way, it is included in the JDK.

Installing the package

Installation is no different from other applications. For example, let's take the package installation JDK.

Environment Variable Settings

Unfortunately, the installer does not make all the necessary settings, so variable environment must be configured manually.

First of all, you will need to indicate variablejava_ home, which contains the path to the folder where Java is installed. This variable use many programs to watch location Java.

Then you need to put it into a variable path register the location %JAVA_HOME%\bin. It is used by the operating system to see a list of folders where the executable file can be found. She also used to run Java from the console.

First you need to go to computer properties. You can do this in any convenient way, for example, call the My Computer context menu and click Properties, or press the combination Win+Pause.

Now let's open Advanced System Settings, and in the next window go to the bookmark Additionally. Here we go to Environment Variables(button below).

If the system variables are already entered Java_ home then you need it edit. If it doesn’t exist, then we create a new one. Here we will need set path to installed Java. Eg c:\Program Files\Java\jdk\ or any other where the installation was carried out.

Now you need to make changes to the variable Path. Here we indicate the path to the folder where the executable files are located, for example %JAVA_HOME%\bin.

Now close all windows by pressing OK, saving the changes.

It is worth noting that for the JDK, the installation folder contains numbers– package version. Therefore, every time you update, you need to change the directory in JAVA_ HOME, otherwise the program simply won’t start.

Removing unnecessary files

Now it remains clean system from unnecessary files. To do this we launch command line(cmd). Then we use the command where java. The console should display the Java installation path. If directory not found– this means the path variable is filled incorrectly. However It happens that the system finds several executable files.

This is due to the fact that installer tries incorrectly tune Environment Variables. Those. tries to install them into a folder system32 .

This is bad because... system directory gets clogged and you can expect the program to not work correctly. In this case, follow the path indicated in the console and delete all files: java.exe, javaws.exe and javaw.exe. If some of these files are not found, then delete only those located in the folder.



Chrome browser versions 42 and above. Starting with Chrome version 42 (released April 2015), Chrome has disabled the standard way in which browsers support plugins. More info


Download and Install Java

Try the offline installer package (Windows only)

The offline installer package will often complete successfully even though the online installer package has encountered a problem. The offline installer package file is large and will take longer to download than the online installer.
» Download the Windows Offline installer

Uninstall any non-working Java installations

Remove prior installations of Java that are not working before trying to install again.
» Uninstall Java for Windows
» Uninstall Java for Mac

Temporarily turn off firewall or antivirus clients

Active firewall or antivirus software may prevent Java from installing properly. Remember to turn your firewall or antivirus software back on when you have successfully completed the Java install.

Why do I get file corrupt message during Java installation?

This message is most commonly seen by Windows XP users. Running a Microsoft utility and trying the offline installer may resolve the issue. See the installshield FAQ for more details.

You can test your Java installation using the instructions in the section below.

Use Java

Once you have determined that Java is installed correctly, try the following items to help you run a particular Java application.

Restart your browser after installing Java to enable the new version

If you are still prompted to update Java after installing the latest version, try restarting your browser. Close all of the browser windows, then reopen any web pages that require Java. In most cases starting a new browser session should make the latest installed version of Java active in your browser.
Note: If you are still prompted to update, try restarting your system. Some systems need a restart to register the newly installed Java plugin.

Give permission to run Java, when prompted by the browser

Browsers will ask for your permission to run the Java plug-in in the browser. The permission prompts vary according to the browser.
» Activate Java plug-in in the browser

Check if the Java plug-in is enabled in the browser

Browsers may disable the Java plug-in or related add-ons by default. You may need to configure your browser to enable the Java plug-in.
» Enable Java plug-in in the browser

Verify that Java is enabled in the Java Control panel

If Java is not enabled in the Java Control Panel, you will be unable to run Java applications in any browser.
» Enable Java content in the browser using the Java Control panel

Configure security settings

Ensure that the Java security level is not preventing the application from running. The default security level may restrict the ability to run applications that may present a risk to your computer.
» Configure security level settings in the Java Control panel

How can I run the applications blocked by Security settings?

The Exception Site List feature was introduced starting with the Java 7 Update 51 release. Adding the application URL to the Exception list allows users to run Rich Internet Applications (RIAs) that would normally be blocked by security checks.
» Configure the Exception Site List

Why am I unable to print coupons or shipping labels?

Some Java applications have not been upgraded to meet new Java security standards. As a result, these apps are blocked thus disabling printing. Contact the website or application developer to notify them of the issue.
» Printing issues FAQ

Clear the Java cache (temporary) files

Sometimes a corrupted file in the Java cache can prevent an application from running. You can safely delete the temporary files that Java downloads since Java will download them again and ensure they are up-to-date.
» Clear the Java cache

Check Antivirus or Firewall settings related to Java

Users should check with antivirus security apps and also firewall settings to see if there are areas to configure Java plugins and if they could add trusted sites.

Verify proxy settings

Ensure that you are using the appropriate proxy settings if they are necessary for your particular internet configuration.
» Set proxy settings in the Java Control panel

Uninstall stand-alone JavaFX installations in Firefox (Windows)

A conflict between Java versions 7u10 and above and previous standalone installations of JavaFX can cause Firefox to not recognize installed Java versions.
» See Firefox issues

Test Java

Ensure that Java is enabled in your browser

Once the Java installation has completed, you can verify that Java is working in your browser. It is possible that Java has been disabled or blocked from running in your browser. How you give permission to run Java varies according to the browser being used.