简体   繁体   中英

why c# console command not running on java?

Can anyone tell me why this c# console command not running on Java?

I've made a C# console program as given below:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace face
{
    class Program
    {
        public static void Main(string[] args)
        {
            String path = args[0];
            byte[] imageBytes = File.ReadAllBytes(path);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            Bitmap S1 = new Bitmap(image);
            Console.WriteLine(S1.GetPixel(2, 10));
        }
    }
}

When I run Program.exe image.jpg , I get:

Color [A=255, R=128, G=32, B=128]

Then I created a simple Java application to run the Program.exe executable:

class project 
{   
 public static void main(String[] args)
 {
    String comman="C:\\WINXP\\system32\\Program.exe Sunset.jpg";
    try 
    {
     Process process = Runtime.getRuntime().exec(comman);
     System.out.println(process.getOutputStream());
    } catch (Exception e) 
         {e.printStackTrace(System.err);}
 }
}

When I try to run the Java application, I get the following error:

Program.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

You probably want to change

String comman = "C:\\WINXP\\system32\\Program.exe Sunset.jpg";

to

String[] comman = { "C:\\WINXP\\system32\\Program.exe", "Sunset.jpg" };

As a comment said, it's probably because the C# program can't open the file: you should specify an absolute path for it, since getting the actual working directory to work right can be an unnecessary pain in this situation. Also it wouldn't hurt to catch exceptions in the C# program (for example for the "no arguments" and "file not found" cases).

The C# process is a child process of the java process, and killed automatically then the java VM terminates. Because the process is started asynchronously, that will happen immediately. If you're interested in the output, replace

System.out.println(process.getOutputStream());

with (for instance)

InputStream inputStream = process.getInputStream();
int c;
while ((c = inputStream.read()) >= 0) {
    System.out.print((char) c);
}

if not, write

process.getInputStream().close();
process.waitFor();

Apart from what others have said, I don't think that System.out.println(process.getOutputStream()); will output what you want here - namely the output of the executed C# executable.

System.out.println() has no overload for OutputStream , the return type of Process.getOutputStream() . So the System.out.println(Object) overload will be choosen, which will call OutputStream.ToString() , which is not the output of the C# program, but (most likely, bear with me here) the fully qualified typename of the output stream instance.

Check this SO question/answer, for example, for more information.

You need to check path of exe and image file that you have provided. As I check this code and execute it then I get following output java.io.BufferedOutputStream@c17164

Here I provide you your modified code

public class Test
{
    public static void main(String[] args)
    {
        String path="C:\\C#Sample\\ImageDisplayDemo\\ImageDisplayDemo\\bin\\Debug\\ImageDisplayDemo.exe E:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg";
        try
        {
            Process proc=Runtime.getRuntime().exec(path);
            System.out.println(proc.getOutputStream());
        }
        catch(Exception ex)
        {

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM