Call a command in terminal using Java (OSX) - java

I am trying to write Java code to run wget to retrieve an image from a server
I believe that I have wget properly installed. If I type:
wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe
I find the image in my user account folder.
The following Java code was working properly on Ubuntu, but I had to move the project over to OSX (Mountain Lion)
import java.io.*;
import java.io.IOException;
public class runWget
{
public static void main (String args[])
{
String whatToRun = "wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe";
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(whatToRun);
int exitVal = proc.waitFor();
System.out.println("Process exitValue:" + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
When I try to run it on OSX, I get the runtime error:
java.io.IOException: Cannot run program "wget": error=2, No such file
or directory
I would greatly appreciate if someone could tell me what I am doing wrong.

If wget is indeed installed on your OS X system, then try to specify the full path to it.
Try:
which wget
from the command line, then use that fully qualified path in your Java application.
String whatToRun = "/usr/local/bin/wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe";

Pretty obviously, wget is not installed by default in OS X.
Something more interesting is to write functionality like wget your own.
URL url = new URL("http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe");
InputStream in = url.openStream();
OutputStream os = new FileOutputStream(new File("picture.jpe"));
byte byffer[] = new byte[1024];
int nBytesRead;
while ((nBytesRead = in.read(buffer)) != -1)
{
os.write(buffer, 0, nBytesRead);
}
os.flush();
os.close();
in.close();

None of these answers explain what your actual problem is.
The reason Java is failing is that /usr/local/bin isn't on your PATH.
Obviously it is on the path of the bash shell you're running in Terminal. And probably of any new bash shell you start in Terminal (or via ssh, or whatever). That's probably because you've got a line like export PATH=$PATH:/usr/local/bin somewhere in ~/.profile, ~/.bash_profile, ~/.bashrc, or the /etc equivalents.
On linux, all your GUI stuff is a child of a login shell, so putting something in one of those files (as long as you pick the right one) means Java will end up inheriting that PATH no matter how it gets launched. But on Mac, all your GUI stuff is a child of launchd, and any shell you run is just a sibling of your GUI apps, not the parent. So, setting PATH in bash's startup isn't going to affect something launched from the Finder or an IDE or whatever.
Once you understand the problem, you can understand all the different solutions—you can set the default environment launchd gives to user processes, or do the same thing system-wide, or modify /etc/paths, etc.

It looks like wget is not installed on that OSX system. (It isn't on mine either, despite that being a few generations older.) Either install it or find another way to download a picture; Java does have HTTP support built-in natively after all (see the java.net.URL class).

if you get this error one more time , execute command like this :
which wget
Runtime.getRuntime().exec(new String[]{ "/bin/sh" , "-c" ,"/usr/local/bin/wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe" })

Related

Why doesn't cd command work using Java JSch?

I'm just learning Java and Jsch, and I can get it to run other commands but not cd. The error code returned by the SSHManager sendCommand function is not null, but some unreadable string that is different every time (maybe that means it is null not that familiar with inner workings of Java).
Any idea why not? Similar question here JSch - Why doesn't CD work? but unanswered.
I won't copy and paste the whole SSHManager class here - useful answer with complete code here that I'm trying to follow. Run a command over SSH with JSch
Sample code below:
import SSH.SSHManager;
public class src
{
int ERROR = 0;
public static void main(String[] args)
{
String username = "debian";
String password = "temppwd";
String ipadd = "192.168.7.2";
SSHManager ssh = new SSHManager(username, password, ipadd, "");
ssh.connect();
String out = "";
//this doesn't work, printing output as bytes to show how weird it is
out = ssh.sendCommand("cd Desktop");
System.out.println(out.getBytes());
//some other test commands
out = ssh.sendCommand("mkdir test");
System.out.println(out);
out = ssh.sendCommand("ls");
System.out.println(out);
ssh.sendCommand("logout");
}
}
Output from the Eclipse Console (bin and Desktop are already there in root directory):
[B#b065c63
bin
Desktop
test
Each command executed over SSH "exec" channel (what is behind SSHManager.sendCommand) is executed in its own shell. So the commands have no effect on each other.
To execute multiple commands in the same shell, just use an appropriate syntax of your server shell. Most *nix shells use semicolon or double-ampersand (with a different semantics).
In your case, the double-ampersand would be more appropriate.
cd Desktop && mkdir test && ls
See also Multiple commands using JSch.
Though, if your want to read commands output, you will have problem distinguishing, where output of one commands ends and output of the following commands starts. Let alone if you wanted to check command exit code.
Then it's better to execute each command in its own "exec" channel in a way that does not require a context. In your case that means using full paths:
mkdir Desktop/test
ls Desktop
See also How to perform multiple operations with JSch.
Also as you were going to use file manipulation only, you actually should not execute shell commands at all. Use the standard SSH API for file manipulation, the SFTP.
The question has kind of an answer, in the comments. Every command in sendCommand uses it's own 'pipe', so it disconnects and starts over in each one.
A quick solution would be to send multiple commands in one one sendCommand, such as:
out = ssh.sendCommand("cd Desktop; mkdir test; ls; logout");
But the correct way is to use a session, such as https://stackoverflow.com/a/9269234/290036
I answered a similar question Using java jcabi SSH client (or other) to execute several commands in shell
My open-source API Maverick Synergy has a high-level API to execute multiple commands within a shell. Its currently designed for and works well with bash-type shells.

Runtime.getRuntime() restart mac Java IM

My os is MAC OS 10.11.3.
I using Spark as our IM(Instant messaging),but when the IM log out,IM give an error,said:
java.io.IOException: Cannot run program "open": error=2, No such file or directory .
But when Eclipse runs it, it worked well,and when I wrapped it a mac package and installed it and run it, click "log out" button, it throws errors.
In program, when I log out,I launch a new IM,meanwhile,I killed the old IM application.
Furthermore,in terminal,no matter what I run how many times “open -a Spark”,it just open the same IM,
The source as follows:
public boolean restartApplicationWithScript() {
String command = null;
try {
if (Spark.isMac()) {
command = "open -a " + Default.getString(Default.SHORT_NAME);
}
String commands[] = {"open", "/Applications/Spark.app"};
Runtime.getRuntime().exec(commands);
System.exit(0);
return true;
} catch (IOException e) {
Log.error("Error trying to restart application with script", e);
return false;
}
}
The exception seems to be saying that it cannot find the open command. That seems a bit odd, since it is a standard MacOS command. So I suspect that the reason that it cannot be found is that it is not on the PATH.
Try giving the absolute pathname for the open command. Running which open should tell you what it is.
If open is a shell builtin on MacOS, then you will need to create a subshell to run the command.
Thanks for all, i solved it by 2 step :
1, cmdline = { "open", "-na", "Microsoft Excel" }; this add a "n" ,otherwise,it will open the same app 。
2,chmod -R 777 youAppPath,if not do this ,the app can't issue the command

exec not working with java 1.7.21, but in netbeans works fine

I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

Error executing programs/compiling inside IntelliJ, no errors in command line

I recently bought a Macbook Air and it's now running Mountain Lion, but i have some problems running the company's project, the only other person using a mac on work runs Lion on his Macbook Pro and he had no such problems.
As the title says theres no problem compiling the project on the command line, but when i try to compile it inside IntelliJ i get this error
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (requireJS-Optimizer) on project MarfeelTouch: Command execution failed. Cannot run program "node" (in directory "/Users/pedrompg/Documents/Marfeel/MarfeelTouch"): error=2, No such file or directory -> [Help 1]
The problem also happens when i compile it from the command line and try to run the program
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Cannot run program "phantomjs" (in directory "/Users/pedrompg/Documents/Tenants/vhosts/discoverint"): error=2, No such file or directory
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:232) ~[na:1.6.0_35]
at java.util.concurrent.FutureTask.get(FutureTask.java:91) ~[na:1.6.0_35]
at com.marfeel.pressSystem.impl.SectionPressImpl.getAllItemsFromSectionFeeds(SectionPressImpl.java:137) ~[MarfeelPressSystem-1.0.jar:na]
... 29 common frames omitted
It seems that i can't run any command line programs from inside the project
This is how we the phantomJS call:
private Process buildProcess() throws IOException {
Process process;
String[] invocationCmd = getInvocationCmd();
if (executionDirectory != null) {
if (LOG.isDebugEnabled()) {
LOG.info("Invoking PhantomJS with {} in {}.", Arrays.toString(invocationCmd), executionDirectory);
}
process = Runtime.getRuntime().exec(invocationCmd, null,
new File(executionDirectory));
} else {
if (LOG.isDebugEnabled()) {
LOG.info("Invoking PhantomJS with {} in {}.", Arrays.toString(invocationCmd));
}
process = Runtime.getRuntime().exec(invocationCmd, null);
}
return process;
}
the getInvocationCmd() returns the following array
[phantomjs,--load-images=no,--disk-cache=yes,--max-disk-cache-size=1048576,/Users/pedrompg/Documents/Marfeel/MarfeelHub/target/webapp/WEB-INF/classes/whiteCollar.js,marca/marca.js,http://www.marca.com/]
Don't know if i leave any relevant information
We use Maven, tomcat 7, nodeJS, phantomJS 1.5, nginx 1.2.4, java 1.6.0_35 on the project
Hope someone can help, i'm getting really worried about this, already wasted 2 days trying to solve this problem.
Thanks in advance
As you are using Mac, it's most likely environment related issue. Please note that on Mac GUI applications do not inherit Terminal environment variables, therefore if you have adjusted PATH variable and command works from the Terminal, it will not work when you try to run from other applications.
See the related questions about this Mac feature. Pay attention to the second link, Mountain Lion has different behavior for environment variables.
The easiest way to verify that it's the case and workaround the problem is to run IntelliJ IDEA from Terminal:
open -a /Applications/IntelliJ\ IDEA\ 11.app/
This way Terminal environment will be passed to IDEA and commands you can run from Terminal will also run from IntelliJ IDEA.

How to copy a file from Linux System to Windows system using Java program?

How to copy a file from Linux System to Windows system using Java program?
Thanks for your help.
I want to copy file from linux : /inet/apps/test.java to windows System1: C:\apps\test
We can use following program to copy
public static void copyFiles(String fromFile, String toFile ){
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
}catch(Exception e){e.printStackTrace();}
finally {
if (from != null)
try { from.close();} catch (IOException e) {}
if (to != null)try {to.close();} catch (IOException e) {}
}
}
This program is running on linux.
so fromFile = /inet/apps/test. What will be the toFile path. If i use simply C:\apps\test then how applicaiton recognise the target as System1.
Java makes no diffeence between Windows and Linux files. So, as long as you have access to both filesystem in the computer your java program is running, you can just copy them.
I think you are asking about some
properties for the program.
In that case the properties, should
be configurable. You can keep the
properties file in the same
directory as your Java program or in
the class path.
The property file might look like :
windows.filepath = C:\user\somefile.txt
unix.filepath = /inet/apps/test.txt
So when you port environments. You
don't need to change the properties.
If you are asking about how to port
test.java to windows, then just copy
the file to JAVA_HOME directory on
windows and then you are good to go.
Or If you have a Dual boot system.
You can access your linux drive
from windows, but not the other
way around.
If the Unix system has the Window file system cross-mounted (e.g. via an SMB share), you should be able to find the Unix pathname that corresponds to the Windows destination and copy as you are currently doing.
Otherwise, you will need to use a file transfer protocol of some kind to copy the file.
There's no Java magic that allows you to magically write files to a different computer. The operating system has to be set up to allow this to happen.
FOLLOW UP - you asked:
I have no thought about the magic. So my question was how to copy a file from Windows to Linux. Normally we do FTP on unix Without mounting or we use FileZilla tool to transfer happens. Here if we want to do same thing though java then how to do that?
I don't know how I can say this differently to make you understand, but here goes:
Your choices in Java are basically the same:
You can use FTP. For example on the destination machine, turn pathname of the source file into a "ftp://..." URL, and use java.net.URL.connect() to pull it. There are probably 3rd-party Java libraries that you can use to "push" the file to a FTP server.
If your OS is setup with the file systems cross-mount, you can do a regular file copy, much as your code does.
You can use java.lang.System.exec(...) to run some Windows specific command line utility to do the copying.
In all cases, you will need to figure out how to map pathnames between the Windows and Linux worlds.
You can try the copy() method of the java.nio.file.Files class, of course we assume that your application can access both the path on Linux and Windows either by mapping the folders or something similar. For example
Files.copy(Paths.get(source), Paths.get(destination), StandardCopyOption.REPLACE_EXISTING, COPY_ATTRIBUTES);

Categories

Resources