How to initialize CommandLine class? - java

I am working with this command Line class of org.openqa.selenium.os package and wanted to execute a dir command get the output to my java code.
here is, what i have just tried,
String[] cmds={"date","dir"};
CommandLine cl=new CommandLine(cmds);
cl.execute();
System.out.println("The out put is "+cl.getStdOut());
i get this,
Exception in thread "main" java.lang.NullPointerException: Unable to find executable for: date
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:236)
at org.openqa.selenium.os.UnixProcess.<init>(UnixProcess.java:61)
at org.openqa.selenium.os.CommandLine.<init>(CommandLine.java:47)
What i did wrong ? i jsut googled but no luck. you can also check this to know what CommandLine class does.
I am using Windows XP 32 bit and getting the same exception for all the commands.

In computing, CLS (for clear screen) is a command used by the command line interpreters COMMAND.COM and CMD.EXE on DOS, OS/2 and Microsoft Windows operating systems to clear the screen or console window of commands and any output generated by them.
see http://en.wikipedia.org/wiki/CLS_(command)
if you run cmd -c cls you will get your desired result

Related

Translating windows bat file to linux shell script

This is my exact batch file. I have tried to convert it doing some research online and get an error
"Failed to execute child process "/home/pi/Desktop/TeachVal/TeachValLinuxShell" (No such file or directory)
echo off
cls
echo Running TeachVAL II...
set path=%path%;/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
cls
exit
This one is my attempt at translating.
#!/bin/bash
set +v
clear
echo "Running TeachVAL II..."
java -cp ".dir1;dir2;path/home/pi/Desktop/TeachVAL/comm.jar;
path/home/pi/Desktop/TeachVAL/Robot.jar;/home/pi/Desktop/TeachVAL/TeachVAL"
clear
exit
Welcome to Linux--life is good here, but there are a few things that work slightly differently, when compared to Windows.
One difference is that Windows uses semicolon (;) to separate entries in a list of paths, but Linux uses colons (:) for that purpose.
So, the Windows command:
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
would correspond to this on Linux:
java -classpath comm.jar:Robot.jar:TeachVAL TeachVAL
In general, on Linux, semicolons are used to put multiple command lines into a single line. Once you've learned that, I think you can then understand why:
java -cp .dir1;/home/pi/Desktop/TeachVAL/TeachVAL
would be the same as:
java -cp .dir1
/home/pi/Desktop/TeachVAL/TeachVAL
That would run java (with no class to be executed) and then try to run "/home/pi/Desktop/TeachVAL/TeachVAL" which can't be found.
There are many more differences to learn; here's a page that will help you get started: http://tldp.org/LDP/abs/html/dosbatch.html

Jar file class getting executed through command prompt but noClassDefFound when run through shell script object of QTP

Jar file class getting executed through command prompt but noClassDefFound when run through shell script object of QTP
I have written a class to fetch data from websphere MQ and saved it as MQTools.Jar runnable.
I try to create a shell scripting object and use Run command Exception in thread "main" java.lang.NoClassDefFoundError: bec/MQ/Tools/MQClass
Set objShell = CreateObject("Wscript.Shell")
If instr(1,objShell.Environment.item("classpath"), strJavaFilePath, 1) > 0 Then 'javafilepath is path to the runnable Jar
Else
objShell.Environment.item("classpath") = objShell.Environment.item("classpath") & ";"&strJavaFilePath
End If
objShell.Run("cmd /c set classpath="& objShell.Environment.item("classpath"))
intReturn = objShell.Run ("cmd /c java bec.MQ.Tools.MQClass" & " "& strCommandLineArgs, 1, true) 'This line throws noclassdeffound error
I use the same command, copy and paste into a command window, it runs very smoothly without errors. Can anybody please suggest
Got it...
The issue here is even if I add the classpath runtime in QTP, QTP will not fetch the classpath until you restart QTP. For a new machine, this problem will occur, for an old machine i.e. a machine where this script is already run before, the classpath would already have the required value in variable and won't come across this issue. This issue is similar to one I have posted earlier but yet to find a solution. Refresh system variable using vbscript/QTP
The problem statement is 'How to reflect changes in system env variables in QTP without having to restart QTP'
Solution: Might be with some geek but unknown to the world yet.

Confused on how to run the Sedgewick textbook libraries

I ran the Mac installer here:
http://algs4.cs.princeton.edu/code/
which says
The Mac OS X installer downloads algs4.jar to the /Users/username/algs4 folder; adds it to the DrJava classpath; and provides the wrapper scripts javac-algs4 and java-algs4, which classpath in algs4.jar, for use in the Terminal.
I don't see an algs4 folder created in Users/username..not sure whats going on here.
Q. What is the easiest way to execute the main() method in classes that are contained in algs4.jar?
A. If you used our autoinstaller, you can execute with a command like
% java-algs4 edu.princeton.cs.algs4.StdDraw
This command works:
java-algs4 edu.princeton.cs.algs4.StdDraw
but when i try to run
java-algs4 edu.princeton.cs.algs4.Counter
or
java-algs4 edu.princeton.cs.algs4.BinarySearch
I just get errors like:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at edu.princeton.cs.algs4.BinarySearch.main(BinarySearch.java:94)
Try running with command line arguments that are required as input for this program.
java-algs4 edu.princeton.cs.algs4.BinarySearch largeW.txt < largeT.txt
Get the files from the downloaded data as part of your installation.
You can also refer to this link

Java Runtime OR Processbuilder OR Other

I'd like to know what the best alternative is for running command line executables from Java. The Target platforms for the commands are Windows 7(+) and Unix/Linux.
I have a class that currently uses Runtime.exec() along with the enhancements from the JavaWorld StreamGobbler article. It works about 90% of the time on both Windows and Unix. The other 10% of the time I need to extend the class and then fiddle with putting cmd.exe of /bin/sh in front of the command. I've also had to fiddle sometimes between using a single String that has command and arguments to splitting the command and args into a String[] array.
My latest is a new error/exception "java.lang.IllegalArgumentException: Executable name has embedded quote, split the arguments." My current Runtime.exec() class works fine in Eclipse running as a Java application, but once I build it and run from an actually command prompt, it fails with the above exception.
So now I'm reading that we should be using ProcessBuilder to do command line executables to the OS platform.
My question is, what is the best alternative? Runtime.exec(), ProcessBuilder, or some other option? Is there one option that will service both Windows and Unix/Linux? If not, which one works best with Windows? Which one works best with Unix/Linux?
tia, adym
Not sure how to give Bohemian credit, but I used ProcessBuilder...Solution is at :
Java - ProcessBuilder command arguments with spaces and double-quotes fails

Process not found when running from getRuntime.exec() from the JVM

I am trying to run the following code from within Eclipse:
Process process = Runtime.getRuntime().exec("gs");
However I get the exception:
java.io.IOException: Cannot run
program "gs": error=2, No such file or
directory
Running gs from the command prompt (OS X) works fine from any directory as it is on my PATH. It seems eclipse doesn't know about my path environment variable, even though I have gone into run configurations and selected PATH on the environment tab.
In additional effort to debug this issue I tried the following code:
Process process = Runtime.getRuntime().exec("echo $PATH");
InputStream fromStdout = process.getInputStream();
byte[] byteArray = IOUtils.toByteArray(fromStdout);
System.out.println(new String(byteArray));
The output was $PATH, hmm. Can someone nudge me in the correct direction?
you are assuming that exec() uses a shell to execute your commands (echo $PATH is a shell command); for the sake of simplicity you can use System.getenv() to see your $PATH:
System.out.println(System.getenv("PATH"));
EDIT
Often a better and flexible alternative to Runtime.exec() is the ProcessBuilder class.
I had the same issue and i found the problem. The Path Variable in Eclipse had different content than the one from the command Line.
Solution:
Look up for the $Path variable in command Line and copy the content. Then open Run Configuration->Environment and select new. Name: $PATH Value: insert the copied content.
That solved the Problem.

Categories

Resources