• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Executing C++ programs in Java ?

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't this C++ program execute in my code?
I've got an old C++ program (not from me) which takes 2 parameters: an ascii rawInputFile.in input file and parsedOutputFile.out which contains the cleansed data from the raw inputfile.
If I run the Solaris C++ program like this, it works:
Solaris#> $myCPlusProg rawInputFile.in parsedOutputFile.out > aLogFile.log
But if I embed it in Java as follows, throws following exception:
java.lang.NullPointerException at testdeltrdlck.main(Compiled code)

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

can you tell me exactly in which line it throws NullPointerException, in your code..

Regards
Srinivas
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error in command $myCPlusProg rawInputFile.in parsedOutputFile.out > aLogFile.log

java.lang.NullPointerException at deltrdlck.main(Compiled Code)

I was researching this and it seems that Runtime.exec() is rather limited and cannot do a unix redirect.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I don't see a class named deltrdlck here, and I also don't see how this program could throw a NullPointerException, and nothing prints "Error in command", so I suspect you're not showing us the actual code. Second, note that the "$" character demarcates shell variables, and it's exceedingly unlikely that you've got an actual binary whose name starts with a "$" -- Java isn't going to expand your shell variables for you, you'll need to use the actual name of the program!

Finally, as far as redirects go: Java is a cross-platform language, so you can't expect Runtime.exec() to understand the UNIX-specific concepts of output redirection. If you need to do this, then you can execute an instance of the shell, and tell the shell to do it -- i.e.,



Finally, note that in the command you're running, you're trying to redirect the output to a file. But then in your Java code, you're trying to read the output from the program. If the redirection succeeds, then there will be no output, right? You may need to sit and think a moment about what you actually want to do here.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that works.
Also thanks for explaining the significnance of "$".
Whatdoes the "-c" do?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a switch to /bin/sh which tells it to execute the next argument as a command line. Windows CMD.EXE has a /C switch which does essentially the same thing.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic