Hello foums!
How are you? I've been wrestling for like about 2 weeks trying to nail this problem.
the Objective:
I am working on a main class that creates another .java, at the desired location.
The idea is to have my original program, which created the .java file to compile and have the cmd execute its .class version which is at the desired location.
Before you suggest there are different ways to have another class executed, the problem I am facing is a necessary one.
I am trying to create a stand alone main class, that doesn't rely in any other code than the one it has on its own. With that in mind, I am trying to add the abbility for the stand alone class to create another main class, and for it to be executed by the cmd.
Its pretty complicated so far. But how I see it, in the big picture, what I am trying to do is probably something out-of-the-box.
Approches I've tried:
Originally I started with Runtime getRuntime() exec(String command), which basically sends a String which is interpreted as a cmd instruction. So far so good. However, I am only able to compile and execute files that are on the same directory or path my original java program is under. That is as far as I can go.
Common sense is to type in the "cd locationofthefile". So I had two Runtime getRuntime() exec(String command) run one receiving the "cd ..." and the rest receiving the command I was prettending to use. Unfortunately, it did not work.
I then attempted to use Runtime getRuntime() exec(String [] command) which is able to receive as much commands I'd like, and unfortunately sort of instanciates another cmd with its own environment per command. So using "cd ..." would not work.
Got back to the Runtime getRuntime() exec(String command) taking advantage of some operator available in the cmd prompt like "&&" to send a single String with two instructions. However getRuntime() exec() sort of "tokenizes" each command, going back to the problem before.
Then I tried just sending a single "cd ..." see if would work and it doesnt. Apparently "cd" is not a program thus it doesn't recognize the cmd instruction. Also attempted to initiate the cmd first, and afterwards send, the "cd... " instruction, and other commands. Seems to me Runtime getRuntime() exec(String command), and Runtime getRuntime() exec(String [] command) are just dead ends.
What I haven't tried:
So others have suggested using a ProcessBuilder, I really haven't tested it much. I am new at managing exceptions, input and output Streams, and understanding subprocesses and that stuff. I am not skilled in that yet. However the logic of using the ProcessBuilder, from what I've researched, is that we initiate the cmd first, then send "cd ..." as a subprocess or inputstream, and do the same with the desired commands.. So I haven't tried this. I don't know how to.
Also I recently found out about another possible solution: System.setProperty("user.dir", String Location);. I am not sure if its a variable from JVM or CMD. What this basically does is change is the directory (the directory that specifies where a .class file is located before executing it through the cmd). My logic is I could change this variable before compiling or executing the next program. I've tried it however I am getting:
I've placed some S.O.P.s so I'd be able to determine if user.dir was changed, and I can see it did. However java was not able to find the file. I am using Runtime getRuntime() exec(String command) to run the desired commands assuming the user.dir value was changed. But apparently it did not work. Maybe user.dir value is being reset by Runtime getRuntime() exec(String command)? Maybe user.dir is not used by the Runtime getRuntime() exec(String command)? I don't know that. I still think I could test some other things.C:\Users\Andrew\Desktop\Wish List>java Mercury
Was file C:\rmwtdvm.java created ? : true
Was file C:\rmwtdvm.java written ? : true
Current working directory: C:\
javac rmwtdvm.java stderr: javac: file not found: rmwtdvm.java
javac rmwtdvm.java stderr: Usage: javac <options> <source files>
javac rmwtdvm.java stderr: use -help for a list of possible options
javac rmwtdvm.java exitValue() 2
java rmwtdvm stderr: Error: Could not find or load main class rmwtdvm
java rmwtdvm exitValue() 1
Finally, what other people say is this: Java Applications and the "Current Directory" , unfortunately.
the code is the following:
[highlight=java]import java.io.*;
import java.util.*;
// Code Removed //
Key Points of Assistance
- Please don't tell me to give up, or to try something different.
- Could you briefly explain the ProcessBuilder or any comments on it.
- Maybe you've got some knowledge about the user.dir you'd like to share.
- Any other function I could try?
--- Update ---
I forgot to say, I thank all the people that has been helping me out with this problem. I do appreciate it. I been receiving great support from this community to get this project of mine going.