Executing a terminal command in a java progam - java

javap -classpath /Users/amol/Documents/Java/ -l -c a
When I execute the above command in my terminal the output shows the function names in my class along with the local variables
Process process3 = Runtime.getRuntime().exec(new String[]{
"javap","-classpath","/Users/amol/Documents/Java/","-l","-c","a"});
However when I type the above line in netbeans and run it, it only show the function names and line numbers where the local variables have been declared.
Why is there a change in the output for the same command?

Related

Using 'cmd /c dir' vs 'dir'

Using the method
public Process exec(String command, String[] envp, File dir) throws IOException
I am supposed to print the output of the command dir [filepath] but using Java,
and my question is, why do I have to type for the command argument, (cmd /c dir), and not just (dir)?
If I type dir [filepath] in command prompt for Windows, it works, but not in Java.
exec() executes new process, and for that you need executable file to run (i.e. some .exe file, if we talk about Windows). But there is no "dir.exe". "dir" is built-in command of the Windows Command Prompt, cmd.exe, so to run it, you need to run cmd.exe and pass such command to it.
In contrast, for example, in Linux, ls is separate executable, so there you can directly do something like ls /home/myuser instead of sh -c 'ls /home/myuser'.

how to use arguments like -a - d

Sometimes we run java programs in command prompt in this manner
java -jar myjarfile.jar -a "testing" -b "testing2"
How do i know which parameter belongs to which.
My code does not work
String context = System.getProperty("-a")
System.out.println(context) //returns null

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

Having problems running system commands from a java program:

This is giving just the output of ls:
String[] cmd={"bash","-c","ls","-l"}:
ProcessBuilder pb=new ProcessBuilder(cmd);
Whereas this is giving long listing output properly:
String[] cmd={"bash","-c","ls -l"};
In the first code snippet, the -l option is being passed as an argument to bash, and not to ls. Bash interprets the -l option as specifying that it should behave as a 'login' shell.
The argument after -c should include the whole bash script (spaces included) that you want to be executed, so the second code snippet is correct.
The former passes two option flags to bash: -c with argument ls, and -l which according to the manpage causes bash to act as a login shell.
The second passes one option flag, -c, which the argument ls -l as a single string.
String[] cmd={"bash","-c","ls -l"}:
ProcessBuilder pb=new ProcessBuilder(cmd);
The arguements are to bash, so if you want bash to interpert your "command" via "bash", "-c", ...
then the next item needs to be your entire command, aka "ls -l".
Bash will then parse up the "command" and the -l will be sent as a parameter to "ls". Currently it is a parameter to "bash", which is why you're not getting the results you desire.

Java Runtime.exec woes on Linux

I am working on a program in Java designed to be used on a Linux environment that creates a new Java process that runs another Java class, but I am having trouble with it. I have finally fixed all my issues up to this. Invoking
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })
in my Java program returns
/bin/bash: /usr/lib/jvm/java-6-openjdk/jre/bin/java -classpath /home/kevin/workspace/Misc/bin HelloWorld: No such file or directory
in either stdout/stderr. If I try
Runtime.getRuntime().exec(new String[] { "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })
I get a Java exception
Cannot run program "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'": java.io.IOException: error=2, No such file or directory
...
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
And finally, using a simple
Runtime.getRuntime().exec("/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'")
gives me a
-classpath: -c: line 0: unexpected EOF while looking for matching `''
-classpath: -c: line 1: syntax error: unexpected end of file
from stdout/stderr.
Meanwhile, creating a new one line .sh file (and assigning proper permissions) with only this (no #!/bin/bash at the top of the file)
/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'
gives the correct output with no errors.
I understand that the usage Runtime.exec is quite complicated to perfect, and I already solved some big problems I got from it before, but this problem just plain puzzles me (such as Runtime.exec's use of StringTokenizer screwing up any commands that have spaces in them, which is why I invoked the overload that accepts String arrays). However, I'm still getting problems with it and I don't understand why.
Your first attempt was almost correct.
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/kevin/workspace/Misc/bin HelloWorld" })
You don't need the extra quoting because passing individual String arguments effectively quotes it automatically.

Categories