繁体   English   中英

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

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

谁能告诉我为什么这个c#console命令没有在Java上运行?

我已经制作了一个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));
        }
    }
}

当我运行Program.exe image.jpg ,我得到:

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

然后我创建了一个简单的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);}
 }
}

当我尝试运行Java应用程序时,出现以下错误:

Program.exe遇到问题,需要关闭。 我们对造成的不便很抱歉。

你可能想改变

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

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

正如评论所说,这可能是因为C#程序无法打开文件:你应该为它指定一个绝对路径,因为在这种情况下让实际的工作目录正常工作可能是一种不必要的痛苦。 在C#程序中捕获异常也没有什么坏处(例如对于“无参数”和“未找到文件”的情况)。

C#进程是java进程的子进程,自动终止,然后java VM终止。 因为进程是异步启动的,所以会立即发生。 如果您对输出感兴趣,请更换

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

与(例如)

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

如果没有,写

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

除了别人所说的,我不认为System.out.println(process.getOutputStream()); 将输出您想要的内容 - 即执行的C#可执行文件的输出。

System.out.println()没有OutputStream重载,它是Process.getOutputStream()的返回类型。 所以将选择System.out.println(Object)重载,它将调用OutputStream.ToString() ,这不是C#程序的输出,但是(很可能在这里承担我的)完全限定的类型名称输出流实例。

例如,查看 SO问题/答案以获取更多信息。

您需要检查您提供的exe和图像文件的路径。 当我检查这段代码并执行它时,我得到以下输出java.io.BufferedOutputStream@c17164

在这里,我为您提供修改后的代码

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