简体   繁体   English

为什么c#console命令没有在java上运行?

[英]why c# console command not running on java?

Can anyone tell me why this c# console command not running on Java? 谁能告诉我为什么这个c#console命令没有在Java上运行?

I've made a C# console program as given below: 我已经制作了一个C#控制台程序,如下所示:

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: 当我运行Program.exe image.jpg ,我得到:

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

Then I created a simple Java application to run the Program.exe executable: 然后我创建了一个简单的Java应用程序来运行Program.exe可执行文件:

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: 当我尝试运行Java应用程序时,出现以下错误:

Program.exe has encountered a problem and needs to close. Program.exe遇到问题,需要关闭。 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. 正如评论所说,这可能是因为C#程序无法打开文件:你应该为它指定一个绝对路径,因为在这种情况下让实际的工作目录正常工作可能是一种不必要的痛苦。 Also it wouldn't hurt to catch exceptions in the C# program (for example for the "no arguments" and "file not found" cases). 在C#程序中捕获异常也没有什么坏处(例如对于“无参数”和“未找到文件”的情况)。

The C# process is a child process of the java process, and killed automatically then the java VM terminates. C#进程是java进程的子进程,自动终止,然后java VM终止。 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()); 除了别人所说的,我不认为System.out.println(process.getOutputStream()); will output what you want here - namely the output of the executed C# executable. 将输出您想要的内容 - 即执行的C#可执行文件的输出。

System.out.println() has no overload for OutputStream , the return type of Process.getOutputStream() . System.out.println()没有OutputStream重载,它是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. 所以将选择System.out.println(Object)重载,它将调用OutputStream.ToString() ,这不是C#程序的输出,但是(很可能在这里承担我的)完全限定的类型名称输出流实例。

Check this SO question/answer, for example, for more information. 例如,查看 SO问题/答案以获取更多信息。

You need to check path of exe and image file that you have provided. 您需要检查您提供的exe和图像文件的路径。 As I check this code and execute it then I get following output java.io.BufferedOutputStream@c17164 当我检查这段代码并执行它时,我得到以下输出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)
        {

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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