Dear All,

I'm developing a small Java application which will use Espeak to speak certain text. Now I pass the test as command line argument

$espeak -s 50 "a for apple"

It works perfect on command line
Now in my program, I've used

String cmd = "espeak -s 50 \"A for apple\"";

Then I use this string as
Process p = Runtime.getRuntime().exec(cmd);

Instead of playing 'A for apple' this plays onle 'A', I've tried with \\ but it says
a backslash. So what we need to include is a way to let espeak know of space and remaining string. Please help me about how to do this.

Thanks in Advance.

Vaibhav Dalvi

Dani AI

Generated

Brief diagnosis and the practical fix: the problem is not espeak — it is how Java builds the command line. Passing a single quoted command string to Runtime.exec or to ProcessBuilder and hoping the shell will parse quotes/escapes is unreliable on Unix. As suggested, trying to escape spaces inside the Java string produced a literal backslash (exactly what observed). The reliable approach is to give the executable and each argument as separate Java strings so nothing needs shell-style quoting.

A minimal, robust pattern (ProcessBuilder is preferred) — each array element is one argv item:

ProcessBuilder pb = new ProcessBuilder("espeak", "-s", "50", "A for apple");
pb.redirectErrorStream(true);
Process p = pb.start();
int rc = p.waitFor();

Or the older overload:

String[] cmd = { "espeak", "-s", "50", "A for apple" };
Process p = Runtime.getRuntime().exec(cmd);

Other practical notes based on the thread: using a temp file and espeak's file option (the festival workaround used) is perfectly valid when text is already in files. If text is built at runtime, writing it to a temp file and calling espeak -f <tempfile> is a safe alternative. Avoid trying to assemble a single shell string unless invoking an actual shell ("/bin/sh -c ...") — that adds quoting complexity and security risk if the text is untrusted.

Troubleshooting tips: print Arrays.toString(...) of the command array to verify tokens, call pb.redirectErrorStream(true) or drain stdout/stderr on separate threads to avoid deadlocks, and always check the process exit code with waitFor(). These steps make external TTS calls from Java predictable and portable.

Recommended Answers

All 3 Replies

Have you tried escaping spaces using a singular?

String cmd = "espeak -s 50 \"A\ for\ apple\"";

I would think that using \\ would print a literal \.

Have you tried escaping spaces using a singular?

String cmd = "espeak -s 50 \"A\ for\ apple\"";

I would think that using \\ would print a literal \.

Hi,
Thanks for the reply, but I've already done that.

The espeak speaks the string as "a backslash" and that's it. No more reading.
Anyway I've dumped espeak and just half an hour ago got festival working perfect. So i'm using files for the stuff and i'm doing fine (atleast for now)

Thanks again.
Regards,
Vaibhav

salut,
est ce que je peut appelé espeak sous programme perl??
si oui , SVP dit moi comment?

merci d'avance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.