How to Execute a shell command Using Runtime.exec - Java

  Рет қаралды 15,446

HiLow Group

HiLow Group

Күн бұрын

Пікірлер
@ravikishore8091
@ravikishore8091 4 жыл бұрын
Great practical explanation ,reduced my time and difficulty in understanding
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Thanks Ravi. We are glad we could help :)
@JitendraKumar-zs1xq
@JitendraKumar-zs1xq 2 жыл бұрын
java.io.IOException: Cannot run program "ls": CreateProcess error=2, The system cannot find the file specified how to solve this
@ankitagupta6232
@ankitagupta6232 2 жыл бұрын
Hi I have a doubt I'm trying to run rsync command with runtime in java. In my A.class file ``` @autowired B bclass; Public void testFileMove() { String srcPath="src/main/resources/tmp/" String destPath="src/main/resources/destTemp" ReflectionUtils.setField(bclass, "tempSrc", srcPath) ; ReflectionUtils.setField(bclass, "tempDest", destPath) ; bclass.execute(); } ``` In my B.class ``` String tempSrc; String tempDest; Public void execute() { String runtimeCmd= "rsync -avR --remove-source-files "+tempSrc+"/ "+tempDest Process process =Runtime.getRunyime.exec(runtimeCmd) ; Int exitCode=process.waitFor() ; Logger.Info(" Exit code is :"+exitCode) ; } ``` I always get **error code as 23** for above code. When i give source and destination path as full path it works well. Eg:tempSrc=/home/usr/desktop/temp tempSrc=/home/usr/desktop/destTemp I need the path to be in resources folder of project so that its not specific to system path. With resources folder it become generic to project resource folder and this helps my build in bamboo as well. I can't find the answer for it
@jospehfotso4626
@jospehfotso4626 4 жыл бұрын
Great content!!! keep up the good job
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Thank you
@maheshd3506
@maheshd3506 4 жыл бұрын
Where your mentioned linux server details,how it's connecting
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Hi Mahesh, This tutorial was done on my local so no need to connect to an external server. But you could use ssh to connect to a linux server and run the same commands via the java code. Hope that helps!
@manojprasad9110
@manojprasad9110 4 жыл бұрын
Hi Can you please help me to understand why nohup java -jar mainclass is not executing using Javaruntime method ?
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Hi Manoj, Sorry, i dont understand the question. can you be more explicit? is this related to the content in the video?
@mukundam9428
@mukundam9428 4 жыл бұрын
hi can i store output in a string to split the data based on delimiter
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Yes, in line 23, instead of printing it, you can put output in a string and perform any operation (including splitting) allow on Strings on it
@sparrowsamuel8459
@sparrowsamuel8459 3 жыл бұрын
Good! but how do we do if the script is an interactive script?
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Great Question. we will prepare a video tutorial for that as the process is somehow different. Please subscribe so that you will get notify when we post it :)
@geekproductions3009
@geekproductions3009 3 жыл бұрын
How to run .sh file
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
check this piece out ProcessBuilder pb = new ProcessBuilder("src/lexparser.sh", "myArg1", "myArg2"); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } we will do a more explanatory video on it :) Please subscribe to be notify when we post it
@prasannapacifist
@prasannapacifist 4 жыл бұрын
Mass
@hilowgroup4996
@hilowgroup4996 3 жыл бұрын
Thank you Prasanna
@cyclingracing1
@cyclingracing1 3 жыл бұрын
Cool, thanks, how can I execute a jar file with arguments from another directory?
Java Reflection Explained - bɘniɒlqxƎ noiɟɔɘlʇɘЯ ɒvɒᒐ
20:07
Coding with John
Рет қаралды 195 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН
Java TimerTask ⌚
11:50
Bro Code
Рет қаралды 74 М.
Programming Is NOT Enough | Add these 7 skills…
13:19
Travis Media
Рет қаралды 423 М.
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 198 М.
Java compile and run with command prompt 💽
8:36
Bro Code
Рет қаралды 19 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 157 М.
008: Java, Running Executing PowerShell  or Shell commands in Java
55:17
Info.Tech.Journal
Рет қаралды 1,9 М.
How to execute a shell script from Java in Eclipse
8:53
raksrahul
Рет қаралды 16 М.
Java for the Haters in 100 Seconds
2:22
Fireship
Рет қаралды 2,9 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН