C#运行外部批处理和java文件

C#运行外部批处理和java文件,c#,.net,pos-tagger,C#,.net,Pos Tagger,我正在使用Stanford POS tagger应用程序标记大约300个文件中的一些文章。为此,我编写了一个C代码,它将遍历文件并使用标记器 我的代码如下所示: Process thisProcess=new Process(); thisProcess.StartInfo.CreateNoWindow=true; thisProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden; thisProcess.StartInfo.WorkingD

我正在使用Stanford POS tagger应用程序标记大约300个文件中的一些文章。为此,我编写了一个C代码,它将遍历文件并使用标记器

我的代码如下所示:

Process thisProcess=new Process();
thisProcess.StartInfo.CreateNoWindow=true;
thisProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory=@"C:\postagger";
thisProcess.StartInfo.FileName=@"C:\postagger\stanford-postagger.bat";
thisProcess.StartInfo.UseShellExecute=false;
thisProcess.StartInfo.RedirectStandardOutput=true;

if(Directory.Exists(@"C:\brown2")) {
    DirectoryInfo brown=new DirectoryInfo(@"C:\brown2");
    DirectoryInfo brownParsed;

    if(!Directory.Exists(@"C:\brown-parsed"))
        brownParsed=Directory.CreateDirectory(@"C:\brown-parsed");
    else
        brownParsed=new DirectoryInfo(@"C:\brown-parsed");

    FileInfo[] files=brown.GetFiles();

    foreach(FileInfo f in files) {

        Console.WriteLine("Parsing file "+f.Name+" ...");
        thisProcess.StartInfo.Arguments=@"C:\postagger\models\wsj-0-18-bidirectional-distsim.tagger "+f.FullName;
        //Console.WriteLine(thisProcess.StartInfo.Arguments);
        thisProcess.Start();
        thisProcess.WaitForExit();
        //Console.Read();
        StreamWriter sw=new StreamWriter(Path.Combine(brownParsed.FullName, f.Name), false);
        string output=thisProcess.StandardOutput.ReadToEnd();
        //sw.Write(thisProcess.StandardOutput.ReadToEnd());
        sw.Write(output);
        sw.Flush();
        sw.Close();
        //Console.WriteLine("File {0} done!",f.Name);
        Console.WriteLine(output);
    }
}
else
    Console.WriteLine("Dir not found!");

Console.Read();
stanford-postagger.bat看起来像这样:

Process thisProcess=new Process();
thisProcess.StartInfo.CreateNoWindow=true;
thisProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory=@"C:\postagger";
thisProcess.StartInfo.FileName=@"C:\postagger\stanford-postagger.bat";
thisProcess.StartInfo.UseShellExecute=false;
thisProcess.StartInfo.RedirectStandardOutput=true;

if(Directory.Exists(@"C:\brown2")) {
    DirectoryInfo brown=new DirectoryInfo(@"C:\brown2");
    DirectoryInfo brownParsed;

    if(!Directory.Exists(@"C:\brown-parsed"))
        brownParsed=Directory.CreateDirectory(@"C:\brown-parsed");
    else
        brownParsed=new DirectoryInfo(@"C:\brown-parsed");

    FileInfo[] files=brown.GetFiles();

    foreach(FileInfo f in files) {

        Console.WriteLine("Parsing file "+f.Name+" ...");
        thisProcess.StartInfo.Arguments=@"C:\postagger\models\wsj-0-18-bidirectional-distsim.tagger "+f.FullName;
        //Console.WriteLine(thisProcess.StartInfo.Arguments);
        thisProcess.Start();
        thisProcess.WaitForExit();
        //Console.Read();
        StreamWriter sw=new StreamWriter(Path.Combine(brownParsed.FullName, f.Name), false);
        string output=thisProcess.StandardOutput.ReadToEnd();
        //sw.Write(thisProcess.StandardOutput.ReadToEnd());
        sw.Write(output);
        sw.Flush();
        sw.Close();
        //Console.WriteLine("File {0} done!",f.Name);
        Console.WriteLine(output);
    }
}
else
    Console.WriteLine("Dir not found!");

Console.Read();
用法:stanford postagger模型文本文件 e、 例如,斯坦福邮戳模型\left3words-wsj-0-18.tagger sample-input.txt

java-mx300m-cp“stanford postagger.jar;”edu.stanford.nlp.tagger.maxent.MaxentTagger-model%1-textFile%2

问题是:

代码运行它,但它不会运行java命令。我在我的笔记本电脑上试过,它就像一个符咒,它的标签。但由于内存不足,它不会标记大文件。但是在我的PC上,它的功能更强大,它不会运行java

如果我打开CMD并为文件输入带有正确参数的java命令,它就会工作。你知道是什么原因导致它不起作用吗?所有路径都很好,我检查了三遍

以下是我从非工作程序(在我的电脑上)获得的输出示例:

C:\postagger>java-mx300m-cp“stanford postagger.jar;”edu.stanford.nlp.tagger.maxent.MaxentTagger-model C:\postagger\models\wsj-0-18-bidirectional-distsim.tagger-textFile C:\brown2\aaa.txt


我不能百分之百确定是否使用这个来运行批处理文件,但是否可以使用进程来执行它

var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "path-to-file.bat"
            }
        };
        process.Start();
        process.WaitForExit();

笔记本电脑和台式机使用的是同一个bit操作系统吗?这不是bat文件执行,而是批处理文件本身执行,但是java不会继续。当你在普通命令行(cmd.exe)中键入“java-version”时会发生什么?它可以工作。有趣的是,如果我从CMD运行它,它会工作。如果我在cmd中键入“java-version”,它将显示我的java版本1.7.0_17。但是,我将批处理文件(而不是“此处的java参数”)更改为“C:\Program Files\java\jdk1.7.0\u 17\bin\java”arguments here”,结果成功了。奇怪的东西:)“java”是一个环境变量,它无论如何都会执行这个文件。我不相信环境变量是特定于用户的,但这可能是一个问题?但至少它已经解决了。