0

I am starting a new process in java that uses runTime.exec. I copied over some of the code from online, and the classpath it uses does not seem to work for me if i just change the name of the class I am accessing. The line of code:

Process process = runTime.exec(
                       "java -classpath C:\\projects\\workspace\\testing\\bin Program");

The class I am trying to access is Program.java. Is there also a way to make this work for any computer, as long as they have a class named Program?

So what should I write in that area that can refer to the class Program in any computer?

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
APCoding
  • 303
  • 2
  • 19

2 Answers2

0

Before you run a java class, you should firstly compile it.

javac Program.java

It will generate a Program.class file. Then in your code:

// create a file with the working directory we wish
File dir = new File("C:\\projects\\workspace\\testing\\bin");

// create a process and execute java and currect environment
Process process = Runtime.getRuntime().exec("java Program", null, dir);
Jerry Z.
  • 2,031
  • 3
  • 22
  • 28
  • Why do we need to add "java program" in creating a new process, and also where is the actual file in the dir variable? – APCoding Jun 14 '15 at 02:05
0

The class I am trying to access is Program.java. Is there also a way to make this work for any computer, as long as they have a class named Program?

  1. You (or they) need to compile the program. (That is no different to C, C++ and numerous other programming languages.)

  2. They need to have Java installed on their machine.

  3. They need to invoke the command correctly, depending on where Java is installed, where your (compiled) program is, and so on1.

There are various things you can do to simplify this for the end user. For example, you can implement an installer that installs the software in a standard place and provides a launcher or a wrapper script for running it.


Launching a Java application programmatically (as per your example) has a couple of extra problems:

  • You need to know where the java command is installed (unless you can rely on the search path being sane).

  • You need to know where your application's bytecode files and dependent libraries have been installed.

These things are typically dealt with by creating a wrapper script, or by putting the information into an application specific configuration file.


Why do we need to add "java program" in creating a new process ....

Because that is the way that mainstream Java works. The Java compiler (javac) generates "bytecode" files rather than conventional (platform specific) executables. You need the java command to run the bytecode files, 'cos the operating system doesn't know haw to deal with them itself.


1 - The things that need to be correct are: 1) the pathname of the java command, 2) the classpath has to include the class directories and the JAR files that the application requires ... with the correct paths, 3) the classname must be correctly specified. And if you are invoking the command programmatically from Java, then you cannot assume that exec knows how to split a command string into arguments correctly.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216