Assuming you meant Java and not javascript, due to the syntax of your example.

You can use Runtime.getRuntime().exec(String) to execute a shell command. Try passing your command into that method. If this doesn't work, you could try passing a String[] with the structure ["/bin/sh", "-c", **command**].

Answer from Patrick Bell on Stack Overflow
🌐
Conholdate
products.conholdate.app › conversion › sh-to-java
Convert SH to JAVA online - Conversion App
Conholdate SH-TO-JAVA Conversion is cross platform and cross browser conversion app that allow you to convert SH-TO-JAVA in any modern browser (Chrome, Safari, Firefox, Opera, Tor, etc) and on any OS (Windows, Unix and MacOS) despite your PC ...
Discussions

Compiling and Executing a Java File With a Shell Script
I'm trying to use a shell script to compile and execute a java file. The java classes are using sockets, so there is a client.java file and a server.java file, each with their own shell script. I also want to handle the command line arguments within the shell script, not the java classes. More on unix.com
🌐 unix.com
1
0
March 28, 2017
convert a java program into a linux command - Stack Overflow
I have created one java program on my Linux system which indents and formats the given file. I want to make that program work like a command in Linux which will take file names and other options as More on stackoverflow.com
🌐 stackoverflow.com
July 20, 2013
java - converting a shell script to a jar file - Stack Overflow
What I would like to do is to convert this sh file into .jar file. The result jar file will be run by another program. How can I do so? ... If you are not going to modify your shell file, then you can use a java code to execute these commands and then build that into a jar. More on stackoverflow.com
🌐 stackoverflow.com
April 30, 2017
Converting a java program to linux script
I have been given a task to convert a java program into a linux script Give it up ASAP! More on reddit.com
🌐 r/linux4noobs
21
1
March 14, 2024
🌐
Google Groups
groups.google.com › d › topic › comp.lang.java.programmer › DTwNPV2aXpg
Newbie.. How can I convert this Unix Shell Script to Java for learning purposes? TIA!
It is very easy to get the java source from java bytecode. Just look up "java decompilers" on your search engine. Aside from that, if it is running as a CGI script nobody will see your code whether it is a shell script or a java CGI / Servlet program.
🌐
Javainuse
javainuse.com › bash
Online Bash to Bat Converter Tool
Hex Encoder/Decoder Convert text ... patterns visually Regex Tester Debug expressions against sample text XML -> POJO Convert XML into Java classes Bash -> Batch Translate shell scripts to Windows .bat JSON -> TypeScript Generate TS interfaces from JSON...
🌐
AlgoDaily
algodaily.com › convert › shell › java
AlgoDaily - Daily coding interview questions. Full programming interview prep course and software career coaching.
Programming interview prep bootcamp with coding challenges and practice. Daily coding interview questions. Software interview prep made easy.
🌐
Quora
quora.com › How-do-I-run-a-shell-script-from-Java-code
How to run a shell script from Java code - Quora
Answer (1 of 7): Here are multiple ways already suggested in stackoverflow and both are good: How to run Unix shell script from java code? Run shell script from Java Synchronously
🌐
Unix.com
unix.com › shell programming and scripting
Compiling and Executing a Java File With a Shell Script - Shell Programming and Scripting - Unix Linux Community
March 28, 2017 - I’m trying to use a shell script to compile and execute a java file. The java classes are using sockets, so there is a client.java file and a server.java file, each with their own shell script. I also want to handle the …
🌐
Freelancer
freelancer.com › projects › software-architecture › convert-the-bash-scripts-into
Convert the bash scripts into Java | Freelancer
It seems you neet an ETL tool like DataX(Open Source On Github),If you need custom development for DataX, please contact me. Of course, I can help you with convert the bash scripts into Java, too Hope help you
Find elsewhere
🌐
Medium
medium.com › @benweidig › java-for-shell-scripting-5b5af5860c47
Java for shell scripting. How to use Java self-contained shell… | by Ben Weidig | Medium
September 16, 2023 - One of the significant advantages of shell scripts is that they are just a single file that you can execute easily. Trying to get the same easy usage from a compiled language, I started using Go, which compiles into a single executable that’s runnable without any additional framework or setup on any machine (that is was compiled for). But Java is my daily driver at work, and I’m the only one at the office trying to learn Go it might not be the best choice.
🌐
Belief Driven Design
belief-driven-design.com › revisiting-java-for-shell-scripting-7ff3e
Revisiting Java for Shell Scripting | belief driven design
September 21, 2023 - To mitigate, we can “abuse” then env command to provide an environment where $PATH is available: ... That’s it! Put in your Java code, make the file executable with chmod +x MyScriptFile, and you can run it with ./MyScriptFile. Now you have a Java shell script that’s easy to edit and ...
🌐
DEV Community
dev.to › coder4_life › how-to-run-shell-scripts-in-java-4cbd
How to run shell scripts in Java - DEV Community
December 6, 2021 - Running shell scripts from inside Java code using ProcessBuilder in a thread. This solution works on... Tagged with java, programming, tutorial, linux.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › translation-from-java-to-bash-761926
translation from java to bash
October 14, 2009 - Hello guys I'm trying to transcript an encryption algorithm from java to bash... bug get stuck The lagorithm uses the date + a serial number.... so
🌐
YouTube
youtube.com › robinson handy and technology services
Convert Java App to Shell Script; Live Coding Session - YouTube
I have a Java application that I build that is somewhat difficult to maintain. I will be converting the application to a shell script so that it will be easi...
Published   April 27, 2020
Views   345
🌐
CodingFleet
codingfleet.com › code-converter › bash › java
Bash to Java Converter - CodingFleet
Bash to Java Code Converter helps translate code from Bash into Java. It converts the structure, syntax, and common patterns into a practical Java version that you can review, edit, and run in your workflow.
🌐
Reddit
reddit.com › r/linux4noobs › converting a java program to linux script
r/linux4noobs on Reddit: Converting a java program to linux script
March 14, 2024 -

Hey, I have been given a task to convert a java program into a linux script...

Ik basics of linux and I can convert some basic lines of java into linux, but problem arises when custom packages comes into picture...

The java program has some custom packages imported and designed to perform specific task like connecting to a server with credentials such as username and host etc

So my query is how do I manage this custom packages in linux scripts, I could not find anything about this on internet, and even search this subreddit but did not find anything...

Also my lead doesnt want this to run this program through script, she explicitly wants it to convert into a unix script...

🌐
Stack Overflow
stackoverflow.com › questions › 56204198 › convert-java-program-to-shell-script
Convert Java program to Shell script - Stack Overflow
That's what I will have to do if we can't convert java to shell. I thought if there is a plugin that does this, I might use it ... Save this answer. ... Show activity on this post. You can use shell script's variable arguments to achieve this.
🌐
IDRSolutions
blog.idrsolutions.com › home › tutorial – calling java from a bash script
Tutorial – Calling Java from a Bash Script
November 10, 2025 - #!/bin/bash clear echo "---------Starting Script---------" echo echo "---------Generating Vars---------" OUTPUT="OutputFile/" FILE="examplePDF.pdf" echo "---------Making Directories---------" mkdir -p OutputFile/ echo "---------Converting---------" java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT echo echo "---------Finished Script---------" Now we have written our BashScript to convert a PDF file to HTML5 we need to give the file permission to run, so first thing we do is navigate to the directory where our .txt file is located using terminal write:
Top answer
1 of 4
3

I don't know anything about Java, but I can show you a proof of concept. Say we have localfile.txt:

Here is the local file.

and on the remote machine, we have remote.sh:

#!/bin/bash
cat /dev/stdin

Note that the script on the remote machine invokes a program which reads from stdin. Then pass the contents of localfile.txt to your ssh command:

user@local:~$ cat localfile.txt | ssh user@remote remote.sh
Here is the local file.

Your Java program is trying to read a file which does not exist on the remote machine. I guess you could try to mimic a local file.

Change remote.sh to:

#!/bin/bash
cat "$@"

and invoke it with

user@local:~$ cat localfile.txt | ssh user@remote 'remote.sh <(cat /dev/stdin)'
Here is the local file.

I think it would be easier to change that part in your Java program to read from stdin.

I guess things might get messy if localfile.txt contains anything that might be interpreted by the shell as expandable (such as *), but that's for you to figure out.

2 of 4
2

The problem is that the shell redirection (<) sends the file over the ssh tunnel. And the Java class is expecting not the file, but a string with the "filename" of a local file that will be read with a FileReader.

Instead of passing the filename to the FileReader, read from the standard input.

InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader iR  = new BufferedReader (isReader);

Used this as a reference: https://stackoverflow.com/questions/5724646/how-to-pipe-input-to-java-program-with-bash

Now your class will be:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;

public class ReadFirstLine 
{
    public static void main(String[] args) throws Exception 
    {
       String filename = args[0];
       System.out.println(filename);

       //BufferedReader iR  = new BufferedReader (new FileReader(filename));
       InputStreamReader isReader = new InputStreamReader(System.in);
       BufferedReader iR  = new BufferedReader (isReader);

       BufferedWriter oW = new BufferedWriter(new OutputStreamWriter(System.out));

       //outputWriter.write(iR.readLine());
       System.out.println(iR.readLine());

       iR.close();
       oW.close();
    }
}

But for this task I would definitely use instead:

head -1 filename.txt

:)