Java in linux shell

Install Jflex, CUP and Java in Linux Bash Shell

Guide on how to install Java, Jflex and CUP in the Linux operating systems and using bash shell.

Jflex

To install Jflex on Ubuntu linux you can use apt tool, running in the shell the following command:

Java

The Oracle JDK can be downloaded at the following address https://www.oracle.com/java/technologies/javase-jdk13-downloads.html (at the moment of this guide the recommended version to download is jdk-13.0.2_linux-x64_bin.tar.gz ).
Do not use Linux OpenJDK package.

Java installation

To install Java a possible guide is the following:

cd mkdir -p compilers cd compilers
tar xvfz jdk-13.0.2_linux-x64_bin.tar.gz

Change the symbolic links of Java programs (i.e., java , javac and jar ) to the new installation of java with the following commands:

cd /usr/bin/ sudo ln -fs /home/skenz/compilers/jdk-13.0.2/bin/java . sudo ln -fs /home/skenz/compilers/jdk-13.0.2/bin/javac . sudo ln -fs /home/skenz/compilers/jdk-13.0.2/bin/jar .

CUP

To install CUP follow the following steps:

Download CUP

Important: do not use apt tool to install CUP, because it does not work correctly.

Installation

Execute next instructions:

cd mkdir -p compilers cd compilers
wget https://www.skenz.it/repository/compilers/progs/java_cup_v11_draw_tree.zip
unzip java_cup_v11_draw_tree.zip
cd java_cup_v11_draw_tree/java_cup
javac *.java runtime/*.java ../StdDraw.java

Configure environment variables

Before running CUP, the Linux Shell environment variables must be configured. This guide refers to the bash shell. To check the shell you are using, type the following command: echo $SHELL

To set the environment variables, open in your home directory the file .bashrc (the .bashrc file is executed by the shell each time you open a terminal and usually it is used to configure a shell):

After opening the .bashrc file, add on its top the following lines:

PATH=$PATH. /home/skenz/compilers/java_cup_v11_draw_tree/java_cup/ export PATH CLASSPATH=.:/home/skenz/compilers/java_cup_v11_draw_tree/ export CLASSPATH

Note: to make active the new environment variables you must reopen a new shell. Remember that both $PATH and $CLASSPATH environmental variables have to contain the . (dot) that represents the current directory.

Читайте также:  Имена переменных могут включать python

Check installation

To verify that the programs have been installed correctly, open a command prompt (cmd) and enter:

A graphical window should be opened.

For CUP you can type into the command prompt:

and if it is correctly installed you see nothing in the terminal. To stop it you can press CTRL-c .

The installed CUP version contains also the functionality to draw into the screen the parse tree (this version was made especially for this course). To check this part you can type into the command prompt:

java java_cup.MainDrawTree

and if it is correctly installed you see nothing in the terminal. To stop it you can press CTRL-c .

Complete Check of installation

jflex scanner.jflex java java_cup.MainDrawTree parser.cup javac *.java java Main example_expr.txt

Other resources

AUR packet for Arch Linux

For the Arch Linux architecture you need to extract the following archive (i.e., file polito-cup.tgz) and execute the command makepkg -si inside the directory polito-cup , in order to automatically download source codes from the website of the course, compile them, and insert them in a pacman packet, which can be later installed in Arch Linux. The packet also contains some scripts, which are installed within CUP, that automatically configure system variables before executing java commands (i.e., polito-cup, polito-cup-drawTree, polito-cup-javac, polito-cup-java).

(This latter procedure was not verified by the professor. If you found it has some problems, please communicate it to the professor)

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/compilers/install_linux_bash

/web/htdocs/www.skenz.it/home/data/pages/compilers/install_linux_bash.txt · Last modified: 2023/03/20 10:11 by zioskenz

Page Tools

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International

Источник

Java in linux shell

This document instructs you on how to use the Linux shell with Java. For a light-hearted essasy on the command line, you might enjoy Neal Stephenson’s light-hearted essay In the Beginning was the Command Line.

Читайте также:  Java servlet for form

You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. We’ll assume you have already installed these.

You will type commands in an application known as the shell. Since you’re using Linux, we assume you’re somewhat familiar with it.

export PATH=$PATH:~/j2sdk1.6.0.18/bin

Then close your shell and open up a new one. If it does not work, in addition, try repeating the same instructions with the file /u/username/.bash_profile.

setenv PATH ~/j2sdk1.6.0.18/bin:$PATH
[wayne] ~> java -version java version "1.6.0_18" Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0_18-112) Java HotSpot(TM) Client VM (build 1.6.0_18-64, mixed mode, sharing) 
[wayne] ~> javac -version javac 1.6.0_18 javac: no source files . 
[wayne] ~> cd introcs/hello [wayne] ~/introcs/hello> 
[wayne] ~/introcs/hello> javac HelloWorld.java [wayne] ~/introcs/hello>
[wayne] ~/introcs/hello> java HelloWorld Hello, World

If your program gets stuck in an infinite loop, type Ctrl-c to break out.

If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file). You should type this character on its own line.

When I try to run java I get: Exception in thread «main» java.lang.NoClassDefFoundError. First, be sure that HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Also, try running your program with the command line

[wayne] ~/introcs/hello> java -cp ./ HelloWorld

How do I check that my PATH variable is set correctly? Type echo $PATH.

Where can I learn more about the command line? Here is a short tutorial on the command-line.

Источник

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.

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.

Читайте также:  Pattern proxy java example

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.

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.

Источник

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