• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

execute java .class file programmatically

 
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written the following code to create the .class file:



This code runs properly and the HelloWorld.class file does get generated. How do I execute this .class file?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:I have written the following code to create the .class file:



This code runs properly and the HelloWorld.class file does get generated. How do I execute this .class file?




Well, since the class file isn't available at compile time (of the program that wants to execute the code), one option is to use the reflection library.

Henry
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Runtime.exec(...) and java.lang.ProcessBuilder come to mind.
 
Joel Bijapurkar
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim and Henry. I tried using the Runtime and ProcessBuilder option. Here is the code i created:



This program executes without any errors or exceptions but the HelloWorld program does not execute. In my HelloWorld program I am writing to a "out.txt" file, but after executing this program when I open the "out.txt" file it is blank.

The HelloWorld program executes fine through the command prompt. But in the command prompt we use the following command set path = "C:\Program File\Java\jdk\bin" . Do I have to do use this command in this case too? If so, how?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:



We don't do java file_system_path_to_class_file. We do java -cp some_classpath fully.qualified.ClassName

So, if your HelloWorld class is not in a package, and you have C:/HelloWorld.class, then you'd execute java -cp C:/ HelloWorld

If, on the other hand, HelloWorld was in package com.mycompany.hello, and you had, for example, C:/projects/HW/com/mycompany/hello/HelloWorld.class, then you'd execute java -cp C:/projects/HW com.mycompay.hello.HelloWorld

Note that the class must be in a subdirectory path matching the package name, and the parent directory of the root of that subdirectory must be on the classpath.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:
The HelloWorld program executes fine through the command prompt.



If you're doing java C:/HelloWorld like you are in your code, then no, it doesn't.
 
Joel Bijapurkar
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jeff. I tried executing the following command:

But the file is not executing.

Another problem I am now facing is with the following command:

This command works fine in Netbeans but is returning null value when used in the Eclipse IDE.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using an absolute path (for "java").

Note that to function properly you may have to handle the input/output/error streams of the Process, and generally follow the advice given in this article.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:Thank you Jeff. I tried executing the following command:

But the file is not executing.


Unfortunately in Jeff's posts a space coinicided with a newline. The command Jeff was suggesting is
java -cp C:/ HelloWorld
with a space after the c:/
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Joel Bijapurkar wrote:Thank you Jeff. I tried executing the following command:

But the file is not executing.


Unfortunately in Jeff's posts a space coinicided with a newline. The command Jeff was suggesting is
java -cp C:/ HelloWorld
with a space after the c:/



Actually, I was making a different point, but that point was based on my own misunderstanding of something the OP had said, so I won't muddy things any further by trying to unravel it.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Try using an absolute path (for "java").


Which you can use System.getProperty("java.home") for. Add the bin folder, add the java executable, and you have the full path to the java executable of the currently used JVM.
 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:
Try using an absolute path (for "java").



Rob Spoor wrote: Which you can use System.getProperty("java.home") for. Add the bin folder, add the java executable, and you have the full path to the java executable of the currently used JVM.



I used the absolute path for java and it ran for me.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic