Execute java with cmd

How to Compile & Run Java Program Using Command Prompt

This article was co-authored by Yaffet Meshesha. Yaffet Meshesha is a Computer Specialist and the Founder of Techy, a full-service computer pickup, repair, and delivery service. With over eight years of experience, Yaffet specializes in computer repairs and technical support. Techy has been featured on TechCrunch and Time.

This article has been viewed 952,736 times.

While many programming environments will allow you to compile and run a program within the environment, you can also compile and run using Command Prompt. Both Windows and Mac have their own versions of Command Prompt, although it is technically called Terminal on the Mac OS. The process is essentially identical for Windows and Mac.

Compiling and Running

Image titled Compile & Run Java Program Using Command Prompt Step 1

  • To ensure that your file is saved as a .java file, be sure to write .java after the file name, and select All Files from the dropdown extension menu.
  • Make note of where you saved the file on your system.
  • If you are unsure of how to write a program in Java, check out our tutorial on how to program in Java. For the purposes of learning how to compile and run a program, though, you can use any sort of Java program.
Читайте также:  Php connection file name

Image titled Compile & Run Java Program Using Command Prompt Step 2

  • Windows: Press ⇱ Home , then type cmd. Press ↵ Enter to open the Command Prompt.
  • Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.

Image titled Compile & Run Java Program Using Command Prompt Step 3

  • If not, you may need to install the Java Development Kit from their website. It is free and can be found at this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Image titled Compile & Run Java Program Using Command Prompt Step 4

  • For example, if you were operating in C:\Users\Bob\Project and wanted to get to C:\Users\Bob\Project\TitanProject , enter in cd TitanProject and press ↵ Enter .
  • You can see a list of what is in your current directory by typing dir and pressing ↵ Enter .

Image titled Compile & Run Java Program Using Command Prompt Step 5

  • If you have any errors in your program, or if there is difficultly compiling, the command prompt will notify you.
  • Check out our article about how to fix compiler errors in Java for more help.

Image titled Compile & Run Java Program Using Command Prompt Step 6

  • After pressing ↵ Enter , your program should run. If you receive an error, or if your program fails to run in any way, refer to the troubleshooting method.

Troubleshooting

Image titled Compile & Run Java Program Using Command Prompt Step 7

  • Windows: Type java -version into the command prompt and press ↵ Enter . Based off the version of Java displayed on the first line, type in set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin to the command prompt and press ↵ Enter . Substitute jdk1.5.0_09 with whatever version of Java you have installed.
    • Be sure you are entering in this command while in the directory containing your Java project.

    Community Q&A

    What would I do if I type in the Java filename.java and it is not running? The code written in Notepad is correct.

    Thanks! We’re glad this was helpful.
    Thank you for your feedback.
    As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

    Once you are in the correct directory, you can compile the program by typing «javac filename.java» into the command line and pressing enter.

    Thanks! We’re glad this was helpful.
    Thank you for your feedback.
    As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

    Thanks! We’re glad this was helpful.
    Thank you for your feedback.
    As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

    Again, it is important that you have the Java JDK downloaded and installed on your computer. The link is here: http://www.oracle.com/technetwork/java/javase/downloads/index.html.

    You Might Also Like

    Set Java Home

    How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide

    Check Your Java Version in the Windows Command Line

    Use Easy Windows CMD Commands to Check Your Java Version

    Do Division in Java

    How to Do Division in Java (Integer and Floating Point)

    Compile and Run Java Program by Notepad

    How to Compile and Run Java Programs Using Notepad++

    Calculate Percentage in Java

    Add JARs to Project Build Paths in Eclipse (Java)

    Check Null in Java

    Install Java in Ubuntu Using Terminal

    Program in Java

    Create an Executable File from Eclipse

    Call a Method in Java

    Update Java

    Print Double Quotes in Java

    Simple Steps to Type a Bunny with Your Keyboard

    Check Java Version on a Mac

    About This Article

    This article was co-authored by Yaffet Meshesha. Yaffet Meshesha is a Computer Specialist and the Founder of Techy, a full-service computer pickup, repair, and delivery service. With over eight years of experience, Yaffet specializes in computer repairs and technical support. Techy has been featured on TechCrunch and Time. This article has been viewed 952,736 times.

    1. Type ″cmd″ into Windows search.
    2. Right-click Command prompt.
    3. Click Run as administrator.
    4. Type ″cd \path\to\java\program″.
    5. Press Enter.
    6. Type ″javac -filename.java″.
    7. Press Enter.
    8. Type ″java filename.java″.
    9. Press Enter.

    Reader Success Stories

    «There is not anything specifically good in any wikiHow Article. ‘Cause all wikiHow articles are awesome. We all love this site. Excellent Work.» . » more

    Is this article up to date?

    «There is not anything specifically good in any wikiHow Article. ‘Cause all wikiHow articles are awesome. We all love this site. Excellent Work.» . » more

    «I’m working on a Boy Scout Programming merit badge and I needed help with the Java part. I couldn’t compile it.» . » more

    Quizzes

    You Might Also Like

    How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide

    Use Easy Windows CMD Commands to Check Your Java Version

    How to Do Division in Java (Integer and Floating Point)

    How to Compile and Run Java Programs Using Notepad++

    Источник

    Execute java with cmd

    Learn Latest Tutorials

    Splunk tutorial

    SPSS tutorial

    Swagger tutorial

    T-SQL tutorial

    Tumblr tutorial

    React tutorial

    Regex tutorial

    Reinforcement learning tutorial

    R Programming tutorial

    RxJS tutorial

    React Native tutorial

    Python Design Patterns

    Python Pillow tutorial

    Python Turtle tutorial

    Keras tutorial

    Preparation

    Aptitude

    Logical Reasoning

    Verbal Ability

    Company Interview Questions

    Artificial Intelligence

    AWS Tutorial

    Selenium tutorial

    Cloud Computing

    Hadoop tutorial

    ReactJS Tutorial

    Data Science Tutorial

    Angular 7 Tutorial

    Blockchain Tutorial

    Git Tutorial

    Machine Learning Tutorial

    DevOps Tutorial

    B.Tech / MCA

    DBMS tutorial

    Data Structures tutorial

    DAA tutorial

    Operating System

    Computer Network tutorial

    Compiler Design tutorial

    Computer Organization and Architecture

    Discrete Mathematics Tutorial

    Ethical Hacking

    Computer Graphics Tutorial

    Software Engineering

    html tutorial

    Cyber Security tutorial

    Automata Tutorial

    C Language tutorial

    C++ tutorial

    Java tutorial

    .Net Framework tutorial

    Python tutorial

    List of Programs

    Control Systems tutorial

    Data Mining Tutorial

    Data Warehouse Tutorial

    Javatpoint Services

    JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

    • Website Designing
    • Website Development
    • Java Development
    • PHP Development
    • WordPress
    • Graphic Designing
    • Logo
    • Digital Marketing
    • On Page and Off Page SEO
    • PPC
    • Content Development
    • Corporate Training
    • Classroom and Online Training
    • Data Entry

    Training For College Campus

    JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
    Duration: 1 week to 2 week

    Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

    Источник

    How to Run Java Program?

    Java Course - Mastering the Fundamentals

    At first, check that Java is installed in your system and the path is properly set then we will see how to run the java program. If Java is not installed on the system or path is not defined properly, then we can’t run any Java programs on our system. After the JDK installation, compiling and running the java program is very easy.

    A java file is compiled using the javac command which generates a platform-independent bytecode that can be executed in any machine by a Java Virtual Machine (JVM). The bytecode is executed using the java command which executes the bytecode and produces the result.

    How to Run a Java Program Using Command Prompt and Notepad?

    Here we will be using Notepad and Command Prompt to run the Java programs. Let’s see the process step-by-step:

    • Create a new folder.
    • Go to the folder, and make the new Java file in that folder.
      • Press Alt+D, and just type Notepad there.
      • Notepad will open, now paste the java code given below in the Notepad and save it with extension java.

      Let’s see the above steps with images:

      Step 1: I have used the code given below for performing all the below operations. Open the Notepad and copy this code in the notepad and save that file with the extension .java.

      You can see in the below images that, I have written the java code and saved it as test.java. Now the new file will be created with the name test.java.

      Create a Notepad named folder

      Open a Notepad file and copy the Java Code

      Save the file with name test.java

      Step 2: You can see that inside the Art folder, there is a java file with the name test.java.

      Java file is there in the folder

      Step 3: Now press Alt+D , and you will get into the above search bar.

      type CMD

      Step 4: Type CMD and press enter key.

      press enter key

      Step 5: You can see that the CMD is open. Now type javac test.java , as you can see in the below image.

      Type javac test.java command

      Step 6: You can see that a new file has been created with the .class extension . The name of the file would be the name of the class you have used inside your program. In my case, I have used the Scaler as a class name.

      new file has been created

      Step 7: Now again, open the cmd and type the command java Scaler as you can see in the below image.

      Open the cmd and type the command java Scaler

      Step 8: At last, you will see that the content of the file has been displayed .

      Content of the file has been displayed

      The javac Command

      Java programs are compiled using the javac command. It takes .java files as input and generates bytecode.

      The java Command

      The java command is used to execute Java bytecodes. It takes bytecode as input, executes it, and outputs the result.

      Conclusion

      • We have seen how to run a Java program using notepad .
      • Create the Java file using Notepad and run it using java and javac commands.
      • Java programs are compiled using the javac command.
      • The java command is used to execute the Java bytecode generated as a result of compilation.

      Источник

Оцените статью