Java in linux shell

Shell instal manually java 8 linux code example

Solution 1: Java by itself may not be the greatest shell language, because of slow boot times, lack of built-in libraries to work with the operating system, commands and files (I mean, there is even no way to copy a file in Java without resorting to creating two input and output streams and piping the data, yack!). There are plenty of JVM-based languages like, JRuby(ruby), Rhino(javascript), Jython(python), Groovy or Grash, Bean-shell, etc. that work like shell languages and can run other Java-written programs.

Use java as a linux shell script language?

Java by itself may not be the greatest shell language, because of slow boot times, lack of built-in libraries to work with the operating system, commands and files (I mean, there is even no way to copy a file in Java without resorting to creating two input and output streams and piping the data, yack!).

However! There are plenty of JVM-based languages like, JRuby(ruby), Rhino(javascript), Jython(python), Groovy or Grash, Bean-shell, etc. that work like shell languages and can run other Java-written programs. I would recommend you have a look at Grash that is probably exactly what you need.

Some of those language even have a way to pre-load JVM runtime to speed-up boot time (e.g Nailgun for JRuby), so that can solve the slow boot time problem. Also see this post for using Ruby(JRuby) as a shell language.

Take a look at Groovy if you want to use the Java platform as a scripting language.

Booting JVM is usually slow and eats considerable memory (compared to native apps) So it is not good for running many(parallel) small applications(processes) that start and die often. . if that was your intention

How to install java locally, Here I have a particular setting insofar as I run three different major versions of the JDK: 6, 7, 8. For instance, here is my source file for setting the current shell to use Java 8:

Shell command fails from java but works when run manually

If the command works fine on the terminal but not when calling from the Java script, the first thing I would try would be to specify Bash on the command being called, see if this works:

bash -c "gpg --output /main/decrypted-token.txt --passphrase test /main/token.asc" 
/bin/bash -c "gpg --output /main/decrypted-token.txt --passphrase test /main/token.asc" 

Use java as a linux shell script language?, I mean, there is even no way to copy a file in Java without resorting to creating two input and output streams and piping the data, yack! This is not true: FileChannel.transferTo. Either way, Main issue w/ java for a shell script stuff is that the JVM starup takes quite a bit time. The main culprit is of course the disk, …

Читайте также:  Fedora linux install nvidia driver

How can I install java runtime on Android device?

Try Termux , a linux terminal emulator for android.A minimal base system is installed automatically — additional packages are available using the APT package manager. You can use following commands to install java8.

  1. pkg install git
  2. git clone https://github.com/MasterDevX/Termux-Java.git
  3. cd Termux-Java
  4. chmod +x installjava
  5. bash installjava

When installed, run java -version to check, if it’s correcty installed. After that you can run java using Java command.

First of all install termux. Then run following commands :

$ pkg update -y && pkg upgrade -y

And give the storage permission. After that download these 2 files.

JDK-9 : https://archive.org/download/openjdk-9-jre-headless_9.2017.8.20-1_x86_64/openjdk-9-jdk-headless_9.2017.8.20-1_arm.deb

JRE-9 : https://archive.org/download/openjdk-9-jre-headless_9.2017.8.20-1_x86_64/openjdk-9-jre-headless_9.2017.8.20-1_arm.deb

Remember the filenames, and the folder where you downloaded the files.

Go back to termux and run :

Replace foldername with your download folder.

Replace file1 and file2 with actual filenames.

If you don’t want to download and move files manually you can use this commands (not recommended) :

It will download files within your termux home directory.

$ apt-get install -y ./openjdk-9-jdk-headless_9.2017.8.20-1_arm.deb

$ apt-get install -y ./openjdk-9-jre-headless_9.2017.8.20-1_arm.deb

And You’re done. Try typing java or javac . If you don’t see an Error you’re good to go.

$ apt update && apt upgrade $ apt install openjdk-17

Java — How to install the JDK on Ubuntu Linux, $ sudo apt install openjdk-8-jdk Verify the Java installation by running the following command which will print the Java version: $ java -version The output should look like this: Output: openjdk version «1.8.0_191» OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12) …

Remotely login into a linux system using java

Drop all that my friend and use the magnificent jsch: http://www.jcraft.com/jsch/

But if you insist on using a command line tool, then use Apache Commons Exec which is far more reliable, flexible and clear in the same time.

You can configure appropriately ssh e.g. thru .ssh/config , and you could have some public keys in .ssh/authorized_keys . Read carefully some good ssh tutorial, e.g. the ssh tutorial for Linux and many others web pages or even some good ssh book.

Читайте также:  Java linux web server

Notice that you probably don’t need to be root to run a df -h command, so I suggest to avoid being remotely root for that purpose.

Although not best practice, if you really wish to do it through Java as asked, this is a solution to your question.

import java.io.InputStream; import java.io.OutputStream; public class Test < public static void main( String[] args ) throws Exception < String[] cm = < "ssh", "root@192.168.1.10" , "df -h" >; Process p = Runtime.getRuntime().exec( cm ); InputStream in = p.getInputStream(); OutputStream out = p.getOutputStream(); out.write( "password".getBytes() ); > > 

You could decide when to send the password based on what is read from the input stream if necessary.

Macos — How to, 1) brew cask install java 2) java -version java version «1.8.0_131» Java(TM) SE Runtime Environment (build 1.8.0_131-b11) P.S — Cask is an extension to Homebrew that is intended to manage large Mac binaries and graphical applications, but using the Homebrew interface.

Источник

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.

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.

Читайте также:  Red hat enterprise linux repositories

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.

Источник

Compile and execute java via bash shell

javac compiles Java source files into class files — it doesn’t create a Jar. To do that, you need to use the jar command as well (and configure what the main-class is so it can create the appropriate manifest). However, you might like to consider using ant or maven to perform the compilation and jarring up of the class files.

Since you know what works from the command line, why are you doing something different in the shell script? As Greg said, if you want to build a jarfile you can do that, but you need to use the right sequence of commands to do so, and if you want server to be the default main() class of the jar you need to also create a manifest file saying so.

offtopic comment, but «shell code» has a different meaning. 🙂 What you’re writing here is usually called «shell script» or «shell program», though the terms are not that distinct.

2 Answers 2

The typical way to build a Java program into a JAR file from the shell is to use the ant utility.

Its home (and documentation) is here: http://ant.apache.org/

Essentially, you’d write (or use a tool to write) a build.xml in your project folder, and then run ant to compile, bundle, et al.

(Disclaimer: I’m not a fan of Ant, but it is, I believe, the most common/popular tool in the Java universe for this task.)

You can also use traditional Unix Makefiles, if you’re familiar with them. Some rules like:

 CLASSES= Server.class Supporting.class %.class: %.java javac $< %.jar: $(CLASSES) Manifest.mf jar cfm $@ Manifest.mf $(CLASSES) 

You could also use a straight-ahead shell script:

 javac Server.java javac Supporting.java jar cfm Server.jar Manifest.mf Server.class Supporting.class 

However, maintaining this is liable to be a nightmare if your project grows beyond a few files.

There are other tools available, as well, but these are the most typical ones I know of.

Источник

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