[erlang-questions] Best way to interface Erlang with Java code

Michael McDaniel erlangy@REDACTED
Mon Dec 7 04:13:52 CET 2009


 one way ...


 1> L = re:split (os:cmd ("java transform animals.txt ; echo $?"), "\n").
 [<<"Cat">>,
 <<"Shark">>,
 ........                  % a lot of animal names here (more than 1000)
 <<"Wolf">>,
  <<"1">>,<<>>]

 2> Ret = lists:nth( 2, lists:reverse( L ) ) .
<<"1">>
 3> case Ret
 3> of <<"0">> -> do_something ;
 3> _          -> do_something_else
 3> end.
failure


 read up on lists and re modules for different ways of playing with
 the output

~Michael


On Mon, Dec 07, 2009 at 03:37:25AM +0100, zabrane Mikael wrote:
> Hi Michael !
> 
> I found a subtle problem when trying your solution when my program returns a
> text with one or more "\n".
> 
> Eg:
> 1> re:split (os:cmd ("java transform animals.txt ; echo $?"), "\n").
> [<<"Cat">>,
> <<"Shark">>,
> ........                  % a lot of animal names here (more than 1000)
> <<"Wolf">>,
>  <<"1">>,<<>>]
> 2>
> 
> How then can I distinguish between my program output and the error code
> (which is at the end of the list) ?
> 
> Thanks
> Zabrane
> 
> 2009/12/5 Michael <erlangy@REDACTED>
> 
> >  on my Linux box ...
> >
> > $ erl
> > Erlang R13B03 (erts-5.7.4) [rq:1] [async-threads:0] [hipe]
> > [kernel-poll:false]
> >
> > Eshell V5.7.4  (abort with ^G)
> > 1> re:split(os:cmd("java x ; echo $?"),"\n").
> > [<<"Exception in thread \"main\" java.lang.NoClassDefFoundError: x">>,
> >  <<"1">>,<<>>]
> > 2>
> >
> >
> > On Sat, Dec 05, 2009 at 06:27:24PM +0100, zabrane Mikael wrote:
> > > Hi Sean !
> > >
> > > Ok I see. But how then cal I handle error code in Erlang if my Java
> > program
> > > crash or returns something different from error code 0?
> > >
> > > Thanks
> > > Zabrane
> > >
> > > 2009/12/5 Sean Cribbs <seancribbs@REDACTED>
> > >
> > > > zabrane Mikael wrote:
> > > >
> > > >> Hi List !
> > > >>
> > > >> I've a small program written in "Java" which take a filename (input),
> > > >> proceed it, and print out
> > > >> a text (output).
> > > >>
> > > >> What's the best way to be able to access to that program from Erlang?
> > > >> My whish is get back the "output" as an Erlang binary.
> > > >>
> > > >> Thanks
> > > >> Zabrane
> > > >>
> > > >>
> > > >>
> > > > Use os:cmd/1, which returns a string/list -
> > > > http://erldocs.com/R13B03/kernel/os.html?search=os&i=5
> > > >
> > > > Sean
> > > >
> >
> > ________________________________________________________________
> > erlang-questions mailing list. See http://www.erlang.org/faq.html
> > erlang-questions (at) erlang.org
> >


More information about the erlang-questions mailing list