Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems executing a jar file from within java code

Status
Not open for further replies.

posdef

Technical User
Mar 11, 2010
10
SE
Hi,

The problem is as follows, I am writing an analysis software, and as a final step of the analysis I want to give the users to view the analysis done. I have found a nice open source software for that last part and what I'd like to do is to execute that program from within my code so that the user doesn't need to bother with that..

The code as it is today looks like this:
Code:
try {
			String path_2_owlfile = owlFile.getCanonicalPath();
			if (path_2_owlfile.contains("owl")){
				command.add("/usr/bin/java");
				command.add("-jar");
				command.add(path_to_program);
				command.add(path_2_owlfile);
				
				Object[] o = command.toArray();
				String[] cmd = Arrays.copyOf(o, o.length, String[].class);
				
				// debug info
				StringBuilder sb = new StringBuilder();
				for (int i=0;i<cmd.length;i++){
					sb.append(cmd[i]);
					sb.append(" ");
				}
				System.err.println(sb.toString());
				Process child = Runtime.getRuntime().exec(cmd);

note that i use system.err for debug since system.out points to a file (the packages i use in my code create a lot of clutter so I set the out stream to a file to keep the terminal relatively clean)

So what happens is that my code executes without any Exceptions thrown, but the application I want to run is never launched. If I c/p the debug text from system.err to terminal, everything works fine.. What could this depend on, and how would one go about solving it?

Thanks,
 
It's difficult to say since I guess you see a command window flickering without time to read. I'd redirect the output of the command to a file using > to see if there's any error.

Appart from that, did you consider the idea of adding the jar to the classpath of your program and invoke a Java class?

Cheers,
Dian
 
Hey Dian,

thanks for the reply.. Following your suggestion, I added
Code:
				command.add(">");
				command.add("debug.log");
to my code. Still no exceptions or errors, not even a file called debug.log. Frustrating...

Could you elaborate on your secondary suggestion? If I understood correctly you suggest that I include the jar file in the classpath and create an instance within my java code. I am not quite sure how that would work, seeing as the jar file consists of lots of packages and classes..
 
Output redirection using > is a function of cmd.exe/command.com, not of the OS.

The second suggestion by Dian is IMHO spot on, why go through the outside door, when you can just directly interface?
 
Output redirection using > is a function of cmd.exe/command.com, not of the OS.

True, i had forgotten about that..

The second suggestion by Dian is IMHO spot on, why go through the outside door, when you can just directly interface?

I see what you are getting at, though I am not quite sure how I could accomplish that, as they are two separate projects. I'd really appreciate if you could elaborate on that..
 
Let's start with the first problem. I'd use this code to capture the output and see what's going on.



Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top