I have a large number of source commands to execute. So I want to write some Java code to do this. Is there a way to do this in Java?
source command is like this:
mysql> source /home/liova/download/tpch/queries/Q1.sql;
You can execute any shell command using Runtime.exec:
Runtime.exec("mysql db_name < /home/liova/download/tpch/queries/Q1.sql");
You can use Runtime class to execute any commands in java. It executes the command as seperate process.
Related
I need to use the cqlsh copy to command in my java application. (copy keyspace.table (column1, column2) to 'path';)
So, is there a way to execute a cqlsh file with that command or just execute the command in the java code?
Thanks!
Because COPY is cqlsh command, not CQL expression, then only way is to launch cqlsh from the Java code via -e command line switch, for example, you can use Apache Commons Exec library, but you'll need to have cqlsh installed on the machine where your Java program runs.
P.S. COPY not always the best way - DataStax has DSBulk utility that is heavily optimized for performant data loading and unloading.
Can any one tell me how to get status report from shell script to java code.
Use case : I run a shell script from java code and if I get any error in shell script(while running) then I need to send some message from shell script to my java code.
So how I can acheive that.
I'm sure there is a better way, but an easy way to do this is to have the shell script pipe error output to a file, which you then read by the Java application.
I'm assuming you are using a ProcessBuilder launch your shell script.
Once you build the process you can use getInputStream() which will give you the output stream from the Shell Script, then just copy the stream to where you want to go.
There also is a redirectOutput but I have limited experiences using it. I'm assuming it does what I'm describing above.
You can use something like this:
Process proc = Runtime.getRuntime().exec("ls -la")
// To get the error code (0=success)
int outCode = proc.exitValue()
If you need to send something from the script to the application executing it, you may be able to do that writing to stdErr in the script and using proc.getErrorStream in your java program.
I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?
Update:
I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.
~Jacob
You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.
Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:
java -jar jarfile.jar param1 param2 ...
So for example, lets's assume that your program takes two arguments. Your script could then be as follows:
java -jar jarfile.jar %1 %2
I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?
With Launch4J you can wrap a Java program in a standalone executable file. I'm not going to copy their (long) feature list here, but definite highlights are the numerous ways presented to customize the resulting exe, its small size, the fact that it's open source and its permissive license that allows commercial usage.
I have implemented a task in Hive.
But now I need to call a Shell Script which I have written for importing the tables in Hive from SQL Server using SQOOP. In that Shell Script I have written a SQOOP Command for importing the tables in HIve.
I tried to call a Shell Script in one of the Demo Application but as the Program runs no action taken place. I just see the blank console.
Do I need to do something extra in case of Hive?
Please help me out from this.
Thanks.
Try to run /bin/sh /home/....TableToExport.sh.
This explicitly defines the shell that interprets your script. This should work.
If it does not work try to simplify your command line. Start from running simple command like ls or hostname. When it works try to execute something more complicated.
Generally it should work. You are on the right way.
The following doesn't work in Java (an exception is thrown):
Runtime.getRuntime().exec("cd mydir; myprog");
The same works fine in PHP:
exec("cd mydir; myprog");
What exactly is different in Java's implementation and why (it seems more limited at first glance)?
the java exec command does not use the system command interpreter. something like "cd mydir; myprog" depends on the system command line interpreter (e.g. on windows cmd, on linux sh) to split that into 2 separate commands and execute each of them. java does not invoke the system command interpreter, so that does not work. you either need to call each command separately, or invoke the desired interpreter yourself as part of the command line.
I've seen people have problems like this, and I'm sure there are several ways, however the one I've seen most people reply is this. add cmd before it.
Runtime.getRuntime().exec("cmd cd mydir; myprog");
Assuming you're running an applet, not Java in a CLI environment on the server? If so, then your Java runtime is running on the client computer, not the server.
Java also has a better way to handle multiple commands than your semicolon. Instead of using the signature:
Runtime.exec(String)
try using this for each of your commands:
Runtime.exec(String[])
and make each argument of your command an element in the String array.