I'm trying to communicate Java and Cobol. I need to call a Java program (with paramaters) from Cobol.
I read some documentation from Microfocus:
http://supportline.microfocus.com/documentation/books/nx40/dijafc.htm
http://supportline.microfocus.com/documentation/books/nx40/dijaco.htm
But I didn't find a real solution, because I need to call an entire program and not a Java Class.
Thanks in advance.
Below is a sample program that will launch an EXE from within a COBOL97 application.
Check CallEXE demo in http://www.netcobol.com/support/code-samples/
When it comes to Microfocus...
One can not CALL an EXE from a Micro Focus INT or GNT, but you can CALL a Non-mainframe program (Micro Focus dialect in MFE) and issue a shell to DOS and from there either execute a command line that executes the EXE or execute the EXE file directly passed on the Micro Focus CALL (x'91' function code =35).
Also, you will not get back any passed parameters since once the DOS shell is closed, no parms can be returned. So the best way to get parms back is to write them to a file.
I am including a sample program that shows this x'91' FC=35 call. As you can see, you can execute a batch file or a command or an EXE directly.
Working-Storage Section.
1 Cmd-Line-Str.
2 Pic X(45)
* value 'RUN $IMSDIR\PCIMS RUNIMS BMP,DBUTIL,DEMO001T'.
* value 'run lorince'.
value 'dir c:\ /o > d.d'.
2 N-1 Pic X Value Low-Value.
1 Call-Func Pic X Comp-X Value 35.
1 Result Pic X Comp-X.
1 Cmd-Line-Len Pic X Comp-X Value 0.
Procedure Division.
P1.
Display Cmd-Line-Str upon Command-Line
Call x'91' using Result, Call-Func, Cmd-Line-Len
If Result = Zeroes
Display 'Call worked'
End-If
Goback.
I hope the post gives you some more information, I have only mainframe knowledge and haven't tried any of this above.
The link you posted explains very well about how we can instantaite a java class. If you are concerned about parameters, then write the Java Class with parameteric constructor and pass the parameters while you instantiate the Class from Cobol.
If you are confused about Java Class and Java Program, then you need to know that Java programs are compiled into .class files at the most you have executable jars containing .class files. But there is nothing like .exe for java.
Related
I was wondering how I would take additional input into a java program from the very line that I call the program to run from?
The input needs to be something like this:
java program abc.fsm <qwerty.txt
where I could run my program "program"
then save the two bits of text "abc.fsm" and "qwerty.txt" as strings then use them to later to call upon files and parse them
I know you can use terminal/cmd to pass in arguments to a Java program, but can you do it the other way, ie have a Java program pass info back to terminal?
More specfically I want to do something like this:
use terminal to launch and pass in value to a Java program
do stuff based on input and pass back a value
use that value to encrypt a file using terminal
repeat
To get your started:
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method. - Java API
You can basically run anything in terminal using this class:
Process p = Runtime.getRuntime().exec(command, null);
Where command is like ffmpeg -help, for example.
The sort of equivalent of args passed to program in the start of main method is an Integer passed to program exit statement, being the return value of the program:
System.exit(0);
This number can be any Integer.
If you run your program by java yourProgram, you can get exit code by
echo $?
I'm trying to run the program in this link (specifically Plotter.java).in the zip file there's an instruction on how to run them but they don't work .I've read other questions on running a java file from terminal and I've applied those solutions but none worked on this fileceven though I've run other codes without any problems (java -dir or javac ).
how can I run this program?
also I want to run it (the plotter) in eclipse console or a GUI made in eclipse
.
p.s:I havent included any code because the program has about 10 classes and also I'm new to java.
Given the exception you have posted, the issue is because you are not providing java with the correct arguments. The program requires at least three arguments which are doubles.
They are:
minX (the first argument)
maxX (the second argument)
frequency (the third argument).
From the instructions which come with it inside the .zip:
PlotEq:
java PlotEq <min-x> <max-x> <sample-rate> <Expression>
Where:
min-x: is the minimum value of x to begin plotting
max-x: is the maximum value of x to plot up to.
sample-rate: how close points are plotted to eachother. A sample rate of 0.1 is generally acceptable, it means take a sample of the graph at 0.1, 0.2, 0.3, 0.4, etc. Basically determines how much detail to include in the plot
Expression: the equation to plot
example:
java PlotEq -5 5 0.01 "sin(x)"
plots sin(x) between x=-5 and 5, taking samples every 0.01 steps in x.
The issue is because of the missing arguments for min-x, max-x and sample-rate.
I tried to see the code, it seems you need to give double in the command line while you don't, so it tries to read empty array of args. Try to write after the name of class you execute three doubles in the command line, it should work than.
If you want to run the same in eclipse, Use menu of Eclipse: Run -> Run Configurations -> Java Application -> mouse right click -> New -> Arguments -> add some arguments you need.
And please read Instructions file carefully, it explains everything.
In the good old days of C you had int main(...) as the entry function and you could call the executable from a batch file and check %errorlevel% in that batch file which would contain the return value.
In java I compile my java application to something.jar and mark a function like public static void main(String[] rawArgs) as the entry point. I then call java -jar something.jar from the batch file. I can even append command line arguments if I want to.
But now I can't check %errorlevel% since my main function is returning a void.
I suppose this is all perfectly logical given that everything is running in a virtual machine and that is the actual executable rather than something.jar.
I can use System.exit(...) to achieve my original aim.
My question is this: Is there a better way of doing this? Killing off the virtual machine seem heavy handed. What if the code runs server side? Am I missing a cute function like Runtime.SetErrorLevel which does what I want?
System.exit() is the correct way to do what you want - a process can only return an error condition when it exits anyway, so what would be the point in specifying that code anywhere else?
Simply use System.exit() with a non zero parameter to indicate the error level - there's no need to look for an alternative way. If you try to work around it you're just going to end up with horrible native hacks that aren't portable yet accomplish the same thing as System.exit().
Place a call to System.exit() just before the end of your main function.
Declare a native function in java, static native void SetErrorLevel(int level);, generate the JNI headers using javah.exe and implement the function in C which sets the %errorlevel% environment variable in your process.
Then terminate normally.
I have the Java code below running on Unix (both AIX and Linux), but it doesn't work. If I run this code the file q1.01 is not compressed, and I don't get any exceptions thrown (The file q1.01 exists, and I expect to find the file q1.01.Z after the command runs.) At the command prompt if I type "which compress" it reports back with "/usr/bin/compress". If I type the command "/usr/bin/compress q1.01" at the Unix prompt it works fine. Any ideas on what might be wrong?
String cmd = "/usr/bin/compress q1.01";
Runtime.getRuntime().exec(cmd);
[Later edit: the problem was in the initial description; the OP was passing a wildcard and not q.01. So my answer below is wrong, except for the part in bold. I'm leaving it so the comments after it will make sense.]
It's trying to run /usr/bin/compress as the program name without arguments.
There are many forms of the Runtime.exec() method. You're using the .exec(String) version, which just takes the executable. Instead, you need to use the .exec(String[]) array version, which takes the executable in String[0] and the parameters in String[1..].
.exec() wants a String array for passing arguments.
Try
String[] cmd = new String[] { "/usr/bin/compress", "q1.01" };
Runtime.getRuntime().exec(cmd);
Note that .exec does not call the local command shell. That means we have to do, among other things, wildcard expansion and even some argument parsing before calling .exec(). This is why you can't just pass it your full command line.
There were a couple of problems. One was that I had tried using wildcards, and since the shell isn't invoked they weren't being expanded. The other problem was that I had created very small test files like this: "echo 'abc' >q1.01". This file was so small that compress couldn't compress it any further and so left it alone. (Stupidly, I think when I typed in the command at the shell I used a different filename, which did compress.)
Thanks everyone for the answers. It did help!
You probably need to use an absolute path to the file. Capture the output though, to see what the problem is - see this page for info on how to do that.
This site may be able to provide some clues.
If the process input stream is null, I suspect that Java wasn't even able to spawn the subprocess. What does Process#exitValue() return?
I'd recommend using strace to see what actually happens on the system-call level. The actual exec() arguments and return code would be especially interesting to see.