ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Rep:
*Nix Pipe in Java
Greetings. I was given this example and told it does an equivalent of a pipe | in Java.
Code:
import java.io.*;
class PipeExample {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("uname -a");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am attempting to write a front end text to speach reader for Linux. This is just source I wrote to test this concept of being able to pipe stdout of one command to stdin of another in Java. This is what I am trying to accomplish in Java.
espeak -g 20 -a 10 -p 50 -s 160 <text to read> | mbrola /usr/share/mbrola/us1/us1 -- | aplay
I do not know how to complete it at the the end. Can someone give me some pointers?
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReadIt {
public static void main(String[] args) {
if(args == null || args.length != 1) {
System.err.println("Usage: java ReadIt \"text to read\"");
System.exit(-1);
}
String[] espeakcmd = new String[]{"espeak", "-g 20", "-a 10", "-p 50", "-s 160", args[0]};
String[] mbrolacmd = new String[]{"mbrola", "/usr/share/mbrola/us1/us1", "--"};
String[] aplaycmd = new String[]{"aplay"};
Process espeakPs = null, mbrolaPs = null, aplayPs = null;
try {
espeakPs = Runtime.getRuntime().exec(espeakcmd);
mbrolaPs = Runtime.getRuntime().exec(mbrolacmd);
aplayPs = Runtime.getRuntime().exec(aplaycmd);
} catch (IOException ex) {
Logger.getLogger(ReadIt.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader ein, min, ain;
ein = new BufferedReader(new InputStreamReader(espeakPs.getInputStream()));
String line = null, str = null;
try {
while((line = ein.readLine()) != null) {
str += line;
}
} catch (IOException ex) {
Logger.getLogger(ReadIt.class.getName()).log(Level.SEVERE, null, ex);
}
// Need str piped to mbrola as input
}
}
Greetings. I was given this example and told it does an equivalent of a pipe | in Java.
Code:
import java.io.*;
class PipeExample { ...
Hello,
You must get the output stream for every process you run. otherwise, the output streams will fill up and the app will stop working. We have had this situation with an external load to oracle and the program just do nothing after some time of execution as the stream was filled... it's not a bad idea to put this code (from your example):
Code:
ein = new BufferedReader(new InputStreamReader(espeakPs.getInputStream()));
String line = null, str = null;
try {
while((line = ein.readLine()) != null) {
str += line;
}
} catch (IOException ex) {
Also, the page that ntubski has given to you is a good example. Please check if this works =D
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Original Poster
Rep:
Hello ntubski and Enrix. Thanks for the replies. So using that Piper class from the blog, I wrote a my own Java test class using it, and it worked with piping audio. I was able to cat music.wav to lame and have it create an mp3. That was just a test, but then I realized that I needed to be able to simulate a command that has two pipes e.g. (Piper.java handles only 1 pipe)
Code:
espeak <text to read> | mbrola /usr/share/mbrola/us1/us1 -- | aplay
So how would I go about doing that? Should I create 2 instances of Piper.java?
I tried creating a modified Piper.java with a constructor that looks like
You can have many pipes opened at the same time, but you need to have the reference to all of them at the same time:
Quote:
Originally Posted by fakie_flip
Hello ntubski and Enrix. Thanks for the replies. So using that Piper class from the blog, I wrote a my own Java test class using it, and it worked with piping audio. I was able to cat music.wav to lame and have it create an mp3. That was just a test, but then I realized that I needed to be able to simulate a command that has two pipes e.g. (Piper.java handles only 1 pipe)
....
Also, you can send more than 1 argument from the command line, in this form:
java app argument1 argument2 .. argumentx
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Original Poster
Rep:
I like Groovy's way of doing it. I am not sure if it's possible to put Groovy code in my Java App and have it work. I did a little research on that, and one example showed Java calling a groovy script. Since Groovy is backwards compatible with Java, can I compile the Java App with Groovy? I'll try to do it, but ant scripts are setup by Netbeans right now.
Update: Not compiling
Code:
bull•NetBeansProjects/JSpeak/src(master⚡)» javac -cp /usr/share/java/miglayout.jar:. jspeak/JSpeak.java [1:23:06]
bull•NetBeansProjects/JSpeak/src(master⚡)» groovyc -cp /usr/share/java/miglayout.jar:. jspeak/JSpeak.java [1:23:44]
/usr/bin/build-classpath: error: Could not find ../../jvm/java/lib/tools Java extension for this JVM
/usr/bin/build-classpath: error: Some specified jars were not found
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
jspeak/JSpeak.java: 63: unexpected token: espeak @ line 63, column 47.
tRuntime().exec( new String[]{"espeak",
^
1 error
bull•NetBeansProjects/JSpeak/src(master⚡)»
Last edited by fakie_flip; 04-18-2012 at 01:24 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.