Is it possible to pass a C application result (a string) to Java application? Both app. console app.

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

Is it possible to pass a C application result (a string) to Java application?

I mean can C application return an output; a string to a Java application as input(maybe with command line arguments)? How could that be done?

Both app. are console app.s

Best regards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Sure try something like the following:


echo 'x' | java ReadIn
import java.util.Scanner;
 
public class ReadIn {
    public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	while (s.hasNextLine()) {
	    System.out.println(s.nextLine());
	}
	s.close();
    }
}

Open in new window

Java Developer
Top Expert 2010
Commented:
just pass it as a command line arg

    public static void main(String[] args) {
        String firstArgument = args[0];
        String secondArgument = args[1];
        ....
Thanks for your interest CEHJ and objects.
CEHJ:
I really appreciate that you are dealing with my other question which is killing me...

>>echo 'x' | java ReadIn
I couldn't get it. I mean where should i use your pipe? The problem is a C executable will return a string and Java will catch it.

objects:
>>just pass it as a command line arg
oki that's my intention too:) But, E.g.
C:
char * main()
{
char * str[30];
blabla...
return str;
}
Java:
E.g. in netbeans, what should I write to command line arguments section? (even I am not sure the above C code returns something; Above C return is valid, does this C code return that string? I mean, I can print it to console prior to return, but cannot see the return since program terminates.)
what should I write to cmd line argument section?

   public static void main(String[] args) {
        String firstArgument = args[0];
...

}

I am  a little lost...

Best regards.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

> Above C return is valid

No its not. It shouldn't compile. Programs can only return integers.
Mick BarryJava Developer
Top Expert 2010
Commented:
you C app needs to write to standard output
you can then run it

java MyClass `MyApp`
Top Expert 2016

Commented:
>>I couldn't get it. I mean where should i use your pipe?

You use it in the way i showed. In the example, the C program 'echo' writes to stdout. Yours should do the same
You mean I am going to write echo 'x' | java ReadIn in command prompt, in my C code or java code? What is that x anyway? is it the executable of my C program?
Mick BarryJava Developer
Top Expert 2010

Commented:
you don't need a pipe
I think it's not console since I got no class found exception readln error:)
C:\Program Files\Java\jdk1.6.0_07\bin>echo C:\mytext.txt java Readln
It just echoes.
Let me say, what I am going to do:
There is a C program named as dummy.exe which can output a string value. When I run this dummy.exe file, a string is outputted.

So, I wrote the following code, to have it as input with no luck:
//args[0] is the path to dummy.exe file.
System.out.println("args " + args[0]);
String cmd = args[0];
Runtime run = Runtime.getRuntime();
run.load(args[0]);
Process pr = run.exec(cmd);
System.out.println("Exit: " + pr.exitValue());//cannot see it anyway

Could you fix this?

Best regards.
P.S. Also, I am curious about the pipe stuff.
Mick BarryJava Developer
Top Expert 2010

Commented:
If you're stuck with windows then you will need to pipe (unless you just have your java app call the C app directly)
Have you got your C app done and writing to standard output as I suggested above?
Mick BarryJava Developer
Top Expert 2010
Commented:
following shows how to use Runtime.exec() to read the output from your C app

If you want to pass it using a pipe then your java app should do the following:

    public static void main(String[] args) {
       String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
       System.out.println("Recieved "+s);



then run it using something like

dummy | java MyApp
Mick BarryJava Developer
Top Expert 2010

Commented:
I had thought you may go to bed, I searched and found the exact same page. lol...
and I have done it with the following code:
 Runtime rt1 = Runtime.getRuntime();
            Process pr1 = rt1.exec(args[0]);
            String line = null;
            BufferedReader input = new BufferedReader(new InputStreamReader(pr1.getInputStream()));

By the way, in C, when I say char * to be returned, the address is returned. I wish there is a way to capture address itself in Java.

>>dummy | java MyApp
Is it really working with pipe? I couldn't that.
Best regards.
>>No its not. It shouldn't compile. Programs can only return integers.
can return address.
Mick BarryJava Developer
Top Expert 2010

Commented:
> Is it really working with pipe? I couldn't that.

should work, MyApp is the name of the class eg.

import java.io.*;

public class MyApp {
    public static void main(String[] args) {
       String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
       System.out.println("Recieved "+s);
   }
}
ok...I have done.

by the way, I have command line args as 11.11.11.11 aa.bb.cc.dd.ee.ff
It's passing perfectly but while I execute from java -jar he seems to execute but fails...

JARP.jar is calling a shell command.

cd C:\Program Files\Java\jdk1.6.0_07\bin
java -jar "C:\ARP\JARP\dist\JARP.jar" "11.11.11.11 aa.bb.cc.dd.ee.ff"

The issue is I can call java -jar "C:\ARP\JARP\dist\JARP.jar" "blabla.exe" but as very above, failing to pass the strings in cmd prompt. What to do?

Best regards.
Mick BarryJava Developer
Top Expert 2010

Commented:
if you are calling iut like that then to get the args you would use

    public static void main(String[] args) {
       String s = args[0];
       System.out.println("Recieved "+s);
I can get it from netbeans without an issue. No problem and the command executes but when I run the command from console, nothing happens.
Top Expert 2016

Commented:
>>What is that x anyway? is it the executable of my C program?

Yes. It's going to help if you're specific about what each application is
ok, i fix the first original problem. Right now, I try to pass strings 11.11.11.11 aa.bb.cc.dd.ee.ff from cmd line argument of java. There is no problem regarding getting them in netbeans as args[0] and args[1].
But when I try to run:
java -jar "C:\ARP\JARP\dist\JARP.jar" "11.11.11.11 aa.bb.cc.dd.ee.ff". It doesn't have efferct. JARP jar has the intention of setting arp and records as static. The syntax is arp -s 11.11.11.11 aa.bb.cc.dd.ee.ff

The program works in Netbeans without a problem but I got the problem in the command line that java -jar "C:\ARP\JARP\dist\JARP.jar" "11.11.11.11 aa.bb.cc.dd.ee.ff" doesn't working.

Best regards.
Top Expert 2016

Commented:
Well the element of the C program seems to have completely disappeared in your last comment. But ...

>>java -jar "C:\ARP\JARP\dist\JARP.jar" "11.11.11.11 aa.bb.cc.dd.ee.ff"

passes *one* parameter to the app , not two, owing to the quotes
>>Well the element of the C program seems to have completely disappeared in your last comment.
Yes, you are right, I am sorry to change the stuff.
Top Expert 2016

Commented:
Can we get back on track?
Sure thing but I call the executable via jar command with your help but I thought It's bettet to pass params to be more generic. (I don't like to change the question, not my habit but after asking, I feel it would be better to ask that wayi my apologies)

So:
>>java -jar "C:\ARP\JARP\dist\JARP.jar" "11.11.11.11" "aa.bb.cc.dd.ee.ff"
Again not working...the arp command works as follows in shell:
arp -s 11.11.11.11 aa.bb.cc.dd.ee.ff

What should I do? A little trick is what I need I think.

best regards.
Top Expert 2016
Commented:
If you want to pass exactly the same arguments to the jar as to arp, then you need


java -jar C:\ARP\JARP\dist\JARP.jar   "-s" "11.11.11.11" "aa.bb.cc.dd.ee.ff"
that's the tweak:)
By the way, thanks for your interest and patience. I am going to deal with the question on wednesday I think since the machine is not with me.

Best regards.
The question is at first passing the executable and reading it but I thought passing arguments are more effective so both objects and CEHJ solutions are absolutely true.
Top Expert 2016

Commented:
:-) OK - i'm still confused, but glad you're happy ;-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial