Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

third part class  RSS feed

 
Rafael Fagundes
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I�m developing an application that uses a third part class. I�ve looked the documentation and the only way to use it is like this:

java CodVer <par1> <par2> <par3>

That�s means that the business rules where impelmented on the main method of this class (I�ve decompiled and that�s true).
My problem is that I�ll use it in my servlet and an instance of this class will not solve my problem because of this implamantation. How to work with this class on my servlet?

Thank�s for your attention and I�m sorry for my bad english!
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I would recommend this normally -- but if you really have to do it... I guess you can build a String array for the parameters and call the main() method directly.

Of course, if it exits by System.exit(), then you are pretty much stuck.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done exactly that with a 3rd party Java-to-HTML tool. Let it be a lesson to us all to think about main methods that only create an instance of something and call a meaningful method with meaningful parameters so others can do the same.
 
Rafael Fagundes
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Stan is right. Why using java like this?

I did not get the idea on Henry�s solution. How to access the main method and to get it�s returns ? It receives 3 params. and I�m waiting for a return string - that�s what heppens on line command exectution.

I�ve found the following solution:

Process p = Runtime.getRuntime().exec("java Codigo " + request.getParameter("p1") + " " + request.getParameter("p2") + " " + request.getParameter("p3"));

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String cod = br.readLine();

But I�m getting a null value. Something still missing......

Thank�s for your attention,
Rafael.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Stan and I were discussing was the not-recommended technique of doing this...



And it may not work for various reasons -- including if the main method calls System.exit() to exit.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
if the main method calls System.exit() to exit.


Not to derail this discussion further, but if you're desperate enough, adding a SecurityManager can take care of this problem, as well.
 
Rafael Fagundes
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
What Stan and I were discussing was the not-recommended technique of doing this...



And it may not work for various reasons -- including if the main method calls System.exit() to exit.

Henry


Ok Henry, I�ve got your point. What I meant is: why using java like this third party class implementation? Kind of one that do not use methods in a decent way. These kind of solutions force us to do stuffs like your recommendation, insted of elegant solutions.

Thank�s again.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, the author of that code never thought about giving it a public API so others could call it easily. Send him one of these:

Any chance you could decompile the class with main() in it and figure out what it does? If you're extraordinarily lucky it's one line like

new Thing().execute( args );

and you could duplicate it in your own code. Somehow it seems more likely that main() will be a thousand lines long.
 
Rafael Fagundes
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank�s all! It started to work when I used the solution with the Process class! Take a look on the "Process class" that I posted.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!