Run java programs in terminal

How to run a Java Class in Terminal

I have done a lot of research and, I could not find how to solve my problem. I saw that there are a lot of people who ask this question, but still none of them answered it for me. I am a beginner at java and I made a simple calculator in Eclipse.

import java.util.Scanner; public class Calculator < public static void main(String[] args) < Scanner numInput = new Scanner(System.in); double fnum, snum; String operation; System.out.println("First number: "); fnum = numInput.nextDouble(); System.out.println("Second number: "); snum = numInput.nextDouble(); Scanner oper = new Scanner(System.in); System.out.println("Please select one of the following operations: "); System.out.println("+"); System.out.println("-"); System.out.println("/"); System.out.println("*"); operation = oper.next(); switch (operation)< case "+": System.out.println("Your answer is: " + (fnum + snum)); break; case "-": System.out.println("Your answer is: " + (fnum - snum)); break; case "*": System.out.println("Your answer is: " + (fnum * snum)); break; case "/": System.out.println("Your answer is: " + (fnum / snum)); break; >> > 

Later I went into the Workspace folder to find the Calculator.class file. I opened terminal and typed:

Danylo-RIB:~ mac$ java /Users/mac/Documents/workspace/Calculator/bin/Calculator.class 

I followed all the instructions on how to run a class in MacOs Terminal, but all I got for an answer in my terminal instead of my program was:

Danylo-RIB:~ mac$ java /Users/mac/Documents/workspace/Calculator/bin/Calculator.class Error: Could not find or load main class .Users.mac.Documents.workspace.Calculator.bin.Calculator.class Danylo-RIB:~ mac$ 

So my question is, how do I do this? How do I run a class in Terminal? EDIT: Okay, thanks to the people who answered my question!

Источник

How to Run Java Programs in Ubuntu

So, you have started learning Java programming? That’s good.

And you want to run the java programs on your Linux system? Even better.

Читайте также:  Python requests uploading file

Let me show how to run Java in terminal in Ubuntu and other Linux distributions.

Running Java programs in Ubuntu

Let’s go in proper steps here.

Step 1: Install Java compiler

To run a Java program, you need to compile the program first. You need Java compiler for this purpose.

The Java compiler is part of JDK (Java Development Kit). You need to install JDK in order to compile and run Java programs.

First, check if you already have Java Compiler installed on your system:

If you see an error like “Command ‘javac’ not found, but can be installed with”, this means you need to install Java Development Kit.

java compiler check ubuntu

The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:

sudo apt install default-jdk

You’ll be asked to enter your account’s password. When you type the password, nothing is seen on the screen. That is normal. Just enter your password blindly. When asked, press the enter key or Y key.

install jdk ubuntu

The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager. The package name could also be different.

Once installed, verify that javac is available now.

java compiler ubuntu

Step 2: Compile Java program in Linux

You need to have a Java program file for this reason. Let’s say you create a new Java program file named HelloWorld.java and it has the following content:

You can use Nano editor in terminal or Gedit graphical text editor for writing your Java programs.

If there is no error, the above command produces no output.

When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.

Читайте также:  Python повторный ввод данных

Step 3: Run the Java class file

You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac.

This will print Hello World on the screen for my program.

running java programs in linux terminal

And that’s how you run a Java program in the Linux terminal.

This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program. Things get complicated for bigger programs and projects.

This is why I advise installing Eclipse on Ubuntu for proper Java programming. It is easier to program in an IDE.

I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.

Источник

How do I run a Java program on Terminal (Mac)?

I am new to Java (as of today!) and am trying to run a very simple program in terminal. Normally, when I run a python (still pretty new) program in Terminal I would simple type in «python name.py» in to terminal and it would run. When I type «Java name.java» it does absolutely nothing. I opened TextWrangler and selected «Run in Terminal» and it returned this error: «This file doesn’t appear to contain a valid ‘shebang’ line (application error code: 13304)» My program is named «hello.java» and it contains the code below. What am I doing wrong?

3 Answers 3

Open the terminal, go to the directory where the file is located and do this:

javac -classpath . name.java // compile the code java name // run the program 

Of course, both javac and java must be available in the operating system’s PATH variable, but in a Mac that’s already configured.

Читайте также:  Java текущий день недели

Apparently I’m just dense. I went to the directory and typed: javac -classpath.hello.java java hello I tried to type java hello on a new line but terminal obviously tries to run when I hit enter.

@Moose What’s the question? you’re describing the normal behavior. You have to press enter after each line, so the command executes

I typed in: javac -class.hello.java and hit enter. Terminal returned «Unrecognized option: -classpath.hello.java Error: Could not create the Java Virtual Machine. Error: Afatal exception has occurred. Program will exit.»

@Moose type the command exactly as it is in my answer (just replace the name of the class), of course it fails: you wrote something different.

@Moose Then read the rest of my answer: the commands must be in the PATH variable of the operating system. It’s time to read the documentation.

javac helloworld.java java helloworld 

The first line calls the compiler and compiles it in the current directory then the next line runs it

«Run in terminal» attempts to run the software as an interpreted script. On UNIX-like systems (meaning almost everything except Windows), a shebang line is used to indicate, what interpreter is to be used. It consists of the characters #! followed by the command to invoke the interpreter (e.g. #!/usr/bin/python or #!/usr/bin/ruby ). However, Java is not (only) interpreted.

java only runs compiled Java bytecode, so it does not work on source files

Instead, head over to the terminal yourself, compile the code with javac and run the resulting bytecode (the .class files) with java :

javac the_file_name.java java the_class_name 

where the_file_name.java is the file containing main(. ) and the_class_name is the name of the class containing main(. ) (this should usually be the same as the_file_name )

Источник

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