Running shell commands

Perhaps you are writing a program in one language and then want to call something that is written in another language. One way of doing this is to run a shell command. Here are ways of running the shell command pdftotext my-fancy-document.pdf in many different languages.

C Sharp

Here's the simple way.

string strCmdText;
strCmdText = startInfo.Arguments = "/C pdftotext my-fancy-document.pdf";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

Do this to hide the cmd window.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";

// "/C" runs and then exits without waiting for user input.
startInfo.Arguments = "/C pdftotext my-fancy-document.pdf";

process.StartInfo = startInfo;
process.Start();

// Wait for the process to end before continuing with the program.
process.WaitForExit()

(I got these from here.)

Java

ProcessBuilder pb = new ProcessBuilder('pdftotext', 'my-fancy-document.pdf');
Process p = pb.start();
p.waitFor()

Nodejs

var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
  // Blah blah blah
}
exec("pdftotext my-fancy-document.pdf", callback);

PHP

exec("pdftotext my-fancy-document.pdf");

Python

import subprocess
process = subprocess.Popen(['pdftotext', 'my-fancy-document.pdf'])
process.wait()

Shell

echo 'pdftotext my-fancy-document.pdf' | sh