i work now in a project of web vulnerability scanner so i would run my perl script into my java the perl script is the uniscan open source tool from kali linux so when i click go to run the script this message appear
Can't locate Uniscan/Crawler.pm in #INC (you may need to install the
Uniscan::Crawler module) (#INC contains: ./Uniscan C:/Perl64/site/lib
C:/Perl64/lib .) at C:\uniscan\uniscan.pl line 25. BEGIN failed--compilation
aborted at test\uniscan.pl line 25.
However when i run hello world script it appear correctly with no probleme in my console. So this is my code of calling perl script in my java
try {
String[] commande = {"perl", "C:\\uniscan\uniscan.pl"};
Process p = Runtime.getRuntime().exec(commande);
AfficheurFlux fluxSortie = new AfficheurFlux(p.getInputStream());
AfficheurFlux fluxErreur = new AfficheurFlux(p.getErrorStream());
new Thread(fluxSortie).start();
new Thread(fluxErreur).start();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
and the code of uniscan is here
Please i need help i'm blocked is for my final degree next week
The program relies on the script's directory being found in the module search path (#INC), but does not ensure this.
If you run the script from the script's directory, it works because . is in #INC by default. But you are running the script from a different directory.
Remove the following useless line:
use lib "./Uniscan";
Replace it with the following:
use FindBin qw( $RealBin );
use lib $RealBin;
Related
I've created a java GUI using NetBeans v.8.2. Very new to Java.
One of the buttons in the GUI launches a shell script (I am aware that this is not ideal Java practice, but it is appropriate for my use case) using arguments gathered from other buttons/text fields in the GUI:
```
private void RunMacActionPerformed(java.awt.event.ActionEvent evt) {
String command[] = {scriptDirStr + "/./Master_run.sh",
projDirStr+"/",
DestDirStr+"/",
ECnonNormStr,
ECnormStr,
ProjID.getText(),
scriptDirStr +"/"};
System.out.print(Arrays.toString(command));
ProcessBuilder pb = new ProcessBuilder(command);
try {
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
```
So, the idea is to launch Master_run.sh with a bunch of arguments.
Master_run.sh runs other R and python scripts, e.g.:
python2 $script_Path/array_data_extractor.py $spath >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt
and
Rscript $script_Path/1_APS_generator_master.R $spath $dpath $APS_src_filename $project_ID $APS_norm_src_filename >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt
and ends with
cat $spath/masterErrors.txt| mail -s $Project_title" done" myEmailAddress#gmail.com
I know the script gets launched because I get an email with the following errors:
"...line 14: python2: command not found"
and
"...line 16: Rscript: command not found"
When I run Master_run.sh with the same exact arguments from within the terminal, there are no such errors. Does anybody know what might be going wrong and/or how to fix it?
To rephrase the problem, it seems I am getting different behavior launching the same commands from within java vs. directly onto the command line.
Your shell environment is clearly different from java's environment. Try specifying the full path to python2 and Rscript. For example
/usr/local/bin/python2 $script_Path/array_data_extractor.py ...
I'm trying to copy a file from /path/to the/file.ext (yes, it has spaces, I suspect this is at least part of the trouble) to /data/data/com.my_pkg.app/file.ext.
In my (root) app, I do:
String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";
try {
Process process;
process = new ProcessBuilder(cmd).start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
But this raises an IOException: no such file or directory.
Checking my sanity, I opened a root file explorer app, navigated to the path above, copied it, and pasted it in my app's directory, and everything was fine.
Why is cp not finding it? - the file explorer app must be doing the same (well, with modification that makes it work!) thing underneath all that GUI.
Edit: the full error:
W/System.err( 2441): java.io.IOException: Error running exec().
Command: [su -c "cp /path/to\ the/file.ext /data/data/com.my_pkg.app/file.ext"]
Working Directory: null
Environment: [ANDROID_ROOT=/system,
EMULATED_STORAGE_SOURCE=/mnt/shell/emulated,
LOOP_MOUNTPOINT=/mnt/obb,
EMULATED_STORAGE_TARGET=/storage/emulated,
ANDROID_BOOTLOGO=1, LD_LIBRARY_PATH=/vendor/lib:/system/lib,
EXTERNAL_STORAGE=/storage/emulated/legacy,
ANDROID_SOCKET_zygote=10,
ANDROID_DATA=/data,
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin,
ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec,
BOOTCLASSPATH=/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/webviewchromium.jar,
ANDROID_PROPERTY_WORKSPACE=9,0,
ANDROID_STORAGE=/storage]
I'm posting this as an answer, because it resolved my question as I asked it, and so should solve this problem for any one happening across this by search. For me, however, it just led to another question.
By changing the string:
String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";
to an array (that the rest of the code as-is concatenates/builds):
String[] cmd = {"su", "-c", "\"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\""};
All the errors were resolved. I can't tell you why, but at least they are.
I am trying to call a perl script from java runtime. It worked fine on my windows7 laptop with the following code,
try {
String cmdString= "c:\\perl64\\bin\\perl.exe c:\\perl64\\eg\\userinput.pl \""+arg1+"\" \""+arg2+"\"";
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
The perl script runs and produces what I expect (update database).
When I move the whole thing over to a remote CentOS server, it doesn't work anymore. The script is the same and the java code is,
try {
String cmdString= "/opt/lampp/bin/perl /home/support/scripts/userinput.pl \""+arg1+"\" \""+arg2+"\" > /tmp/userinput.log";
log(cmdString);
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
I added redirect to /tmp/userinput.log after I see the script is not working. But there is no log file created at all. I also added log to make sure this part of the java code did get executed, and indeed it did. I also tried to add "/bin/bash " in front of the comString and it didn't make a difference. However, when I run the cmdString directly on the remote server from command line, it works without problem.
Now, when I changed the cmdString to "touch /tmp/userinput.log", it does create the empty log file.
So I know the Runtime.getRuntime().exec(cmdString) command ran, and the cmdString works when entered on command line, and a simple "touch" command would work with this setup. But I am totally lost why the actual cmdString that calls the perl script doesn't work, and there is no message whatsoever to tell me what is wrong.
Can someone please help?
Frist, separate each parameter for the command and use the version of exec which takes a String[] (you won't have to worry about quoting issues). also, shell redirection won't work since java isn't executing a shell.
I wrote a C program that simply delete the folder called myFolder.txt
I want to execute the .exe file from a java application.
So, I used the following code:
try
{
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("program2.exe") ;
p.destroy() ;
}catch(Exception exc){/*handle exception*/
System.out.println("ERROR");
}
When I run my java application no error appears but the file is not deleted.
Why?
You have created a process, and then immediately destroyed it. Of course the executable won't run. Try calling .waitFor() instead (or just let it run).
I think that program2.exe might not be in the class path of the Java project.
try {
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("program2.exe") ; // #1
//p.destroy() ; // #2
} catch (Exception exc) {
/*handle exception*/
System.out.println("ERROR");
}
#1 - check path of the exe file.
#2 - no need to destroy the process manually, it will end automatically after completing its process.
You can check whether the the process is started or not, run the Java project - immediately go to task manager - process - if there is a process running called program2.exe, your process is started otherwise it is not started. If not started, there is no exception - then the exe file path is a problem, try with giving full path of the exe file.
I tried to run a shell script from java code, but I am facing problem. The script is in batchstart.sh file -
#!/bin/ksh
export DISPLAY=:0.0
Now the script is run with a dot on the command line -- . batchstart.sh
How do I run it from java? My java code is below. It throws the following exception -
java.io.IOException: .: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:102)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at SetDisplay.main(SetDisplay.java:12)
import java.io.*;
public class SetDisplay {
public static void main(String[] args) {
File wd = new File("/myhomedir/");
System.out.println("Working Directory: " +wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How do I make the shell script run ?
I tried the following code as well, but that too doesn't work.
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /home/");
out.println(". batchstart.sh");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
When run from the command line, using a dot at the start of a script indicates that the script should be run in the current environment, instead of spawning a new subshell and using a new copy of the current environment. This allows you to export a new value of an environment variable to be used by commands run later from the same interactive shell.
Obviously, this technique only works if you are running your batchstart.sh script from an actual shell. Java does not know how this mechanism works and so the dot means nothing to it. A script cannot modify the environment of the Java process it was called from.
If your goal is to change the value of the DISPLAY environment variable for other commands run by your Java process, consider using the ProcessBuilder class to specify a new environment for the child process. Java does not contain a built-in way to modify variables in its own environment.
The source command (".") is a shell built-in. You have to explicitly run /bin/ksh, passing your script name as the argument (followed by any script arguments).
You have a larger problem if you need to source the script. That usually means that environment changes happen in the context of the current shell, not a subshell.
This won't work with Java since Java's not a shell. You'll need to figure out how to change the environment with Java.
Of course, if I'm wrong and there's more to that script that just setting DISPLAY, it may work as suggested.
The method you're going to have to use depends on what you're trying to achieve(as in "Are you running other programs using exec() that rely on DISPLAY being set?" or "Does your Java program need DISPLAY to be set?").
If, as you state in your comment, it's only your Java program that needs DISPLAY set, just set it outside before your program runs. Create a cmd (or bash) file which sets the DISPLAY variable then calls the JRE to run your program.
#/bin/ksh
export DISPLAY-:0.0
/usr/bin/jre/java your_program blah.blah.blah
I would also modify your main() to check that it's set to something and exit gracefully if not:
if (System.getenv ("DISPLAY") == null)
// doesn't exist, exit gracefully.
The period "." is a shell built-in, and executes the script "in-place", analogous to #include in C/C++. Using "." outside of a shell-script has no meaning.
If you want to run the script from Java, you have to execute the script interpreter (/bin/ksh):
Runtime.getRuntime().exec("/bin/ksh batchstart.sh", ...)
but note that this is not semantically equivalent, since you're executing batchstart.sh as a sub-process instead of sourcing it.