I want to be able to manage Powershell PSSessions using Java. For example I want to have separate Java methods for starting sessions and ending them. Is there an easy way to have these methods? I'm thinking there could be a Powershell API, or stuff like that.
Quick answer: No.
Longer answer: You could create one yourself, but you should try and avoid this.
PowerShell is a set of .NET types (the various System.Management.Automation namespaces), there is not even a C API. But you could wrap enough of the PowerShell types in something you could call from Java and then use that. However as you cannot run Java in the same process part of that adapter/proxy layer will need to perform inter-process communications this is not going to be an easy task.
If your requirements were to just be able to run a PSH script then you can launch a PowerShell process with the right command line, eg. to run script.ps1
%windir%\System32\WindowsPowershell\v1.0\Powershell.exe -file path-to-script.ps1
By reading from standard input in the script, and controlling standard input of the child process you could pass commands to a coöperating script:
while (($in = [console]::in.ReadLine()) -ne $null) {
Write-Debug "Input: '$in'"
if ($in -match '^\s*exit\s*$') { break; }
}
Related
We have scripted all of the processes (creating models, table mapping, screnario and loadplan in java using groovy libraries).
Now we want to execute the create loadplan in java.
Somebody please provide the guidelines to script the execution code in java.
We got the method to execute from command prompt.
But looking for option to script in java.
A load plan or scenario execution can be triggered through the class RemoteRuntimeAgentInvoker from oracle.odi.runtime.agent.invocation. Check the invokeStartLoadPlan methods.
Does anyone know if it is possible to call a shell script from within Java program and also pass an argument to that shell script from the for loop of that java class? In my shell script I am setting MySQL system variables to different values to see if those values affect the performance of the database application. I could have set those values through JDBC, but as I am working with MySQL, it is not possible to restart the database from JDBC, after each query execution.
Yes it is possible. For something like this you would probably be better off just using a batch file or something to do it though.
If you really do need to use java try:
How to run Unix shell script from Java code?
Runtime.exec() is what you are looking for.
Runtime.getRuntime().exec("theScript.sh param1 param2);
..modulo exception handling.
You may want to look into Apache Commons
I'm thinking about using pentahose to help me transform different xml files from several sources to integrate the data in my system. Those xml are downloaded from internet every 10 minutes by a java program. If I want to use Kettle to transform the data, do I need a Pentahose server in order to run the transformations? Or is there a way to export the transformations to java classes so I can use them? I'd appreciate any kind of orientation :)
if you wan to run Transformations you only need the command console pan.sh, if you are going to run jobs you will need to execute the kitchen.sh command. Take a peek to carte.sh server, its a self contained webserver that allows you to send transformations and jobs remotely to another machine. (intranet, internet).
Answering your other question about java code: No, kettle does not generate java code based on your transformations and jobs, only xml.
You don't need the server. You can run your Kettle jobs using the Kitchen command line. You could easily setup a cron job to execute your job at a set interval.
http://wiki.pentaho.com/display/EAI/Kitchen+User+Documentation
To run transformations and jobs you need to use pan and kitchen (sh version for linux and bat for windows) read here http://infocenter.pentaho.com/help/index.jsp?topic=%2Fpdi_user_guide%2Fconcept_cli_scripting.html. If you want to run transformation/jobs directly form java, you need to import "lib" and "libswt" (and libext for some versions of kettle) to your java project and use kettle's java API:
KettleEnvironment.init();
JobMeta jobMeta = new JobMeta("job path",null);
jobMeta.setParameterValue("param name",value);
Job job = new Job(null,jobMeta);
job.setLogLevel(LogLevel.BASIC);
job.start();
job.waitUntilFinished();
Result result = job.getResult();
if (!result.getResult()){
//manage the error case
}
this is an example of java API using, kettle is made in java, so it is full integrable
I was hoping someone will be able to give me some advice. I am running a program in java and need to include a function that has already been written in bash. I honestly am not sure where to begin.
I have file that I process to some extent with my java program (I take out duplicates, sort names, etc). I end up with several paths to other files.
for(String go:parsun){
pathed="/home/S/Data/"+go+"/Ana/lysis/"+go+"/file.vcf";
System.out.println("Pathed: " + pathed);
}
I need to take these paths and put them through a bash program which takes the contents of the files and does some math-ish things to their innards.
I have seen around the internet that you could do something like this to run a bash script in java:
Process pro=Runtime.getRuntime().exec("shellfilepath");
BufferedReader read=new BufferedReader(new InputStreamReader(pro.getInputStream());
But I am not sure I understand how that works, or how I would go about passing my file paths to it. Also, another consideration is that I will have to slightly modify the bash script to allow it to take in these paths. Previously the paths were hardcoded before hand. I will also need some suggestions on that front.
Is this the best way to have bash and Java work together? Is there another technique? If not, could someone help me with understanding how to use Runtime?
Thanks
You could use:
Process process = Runtime.getRuntime().exec("bash_command " + pathed);
The pathed filename will be passed in as the $1 argument.
The following looks like a pretty good intro into running shell scripts from java:
http://obscuredclarity.blogspot.com/2011/07/executing-shell-script-from-java.html
It shows how to modify your shell script to accept arguments, and how to use Runtime.exec() to call the script and pass arguments to it.
Also see the API for the Runtime class: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
Runtime.exec(String) is a good way to do this, but you need to be careful about whether or not the path to your script contains spaces. A better approach would be to use Runtime.exec(String[]). This will make sure that Java treats your script path as one command parameter instead of trying to split it on spaces.
Process process = Runtime.exec(new String[] {shellFilePath});
Also, make sure to include #!/bin/bash at the top of your script.
You have two choices:
Rewrite the logic from the bash script in Java. This will execute faster and be less brittle, but if the bash script is large and complex or legacy code that nobody left understands, it may be more work than you want to do.
Execute the bash script using ProcessBuilder; you can pass command-line arguments to it. This comes with its own set of issues having to do with coordinating between two processes. If the bash script does not expect any input things are a little simpler, but in general you need to start the script, run input and output handlers on separate threads, and then wait for termination. This is a little difficult to get right if you've never done it, although there are good examples on the web.
Suppose I have a java program, myProgram.jar, which I have running on a server. To start the program I would type this into the terminal:
>java -jar myProgram.jar
and the program would continue to run indefinitely. Now what about if the program had a function such as
void processInput(String text){
//process the text
}
and I wanted to SSH into the server and call this function with a particular string? so I could log into my server at any time and alter the state of my program. Is this possible?
This can be done, but not easily.
There are standard ways to achieve what you probably want: MBeans. Take a look at http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html
You'll have a number of options here. The simplest would be if you only needed to provide your custom text as an argument at startup to the Java program - in which case you any arguments at the end of your java command would be passed as a String array into your programs main method.
Otherwise, you'll be looking to to implement some sort of remote procedure call (RPC). You could use something like Java RMI (remote method invocation) - where your main execution of your program starts, and you could use child executions of your program (or another client library all together) that calls methods within your main execution while it is still running. If you wanted to extend this further, you could have it host web services over standard HTTP, and use SOAP or REST calls.
There are many additional options and variations here, depending upon your exact requirements.
+1 for DagR's suggestion - again, depending upon exactly what you're looking to do, JMX may be a good fit for this as well.
You could implement a Java client application that calls methods on the Java application running on the server using RMI. Then when you ssh into the server you can run your client application with the parameters you need and have it call the methods in the other program.