मूल पोस्ट एक है कि ज्यादातर लोगों कोजावा में चल रही प्रक्रिया की कार्य निर्देशिका को कैसे पुनर्प्राप्त करें?
का जवाब दे रहे थे यहाँ कोड मैं पहले से ही
String workingDirectory = "/home"; String command = "cd ../"; ProcessBuilder pb = new ProcessBuilder(new String[] { "cmd", "/c", command }); pb.directory(new File(workingDirectory)); pb.redirectErrorStream(true); Process process = pb.start(); // Some time later once the process has been closed workingDirectory = pb.directory().getAbsolutePath(); System.out.println("Path: " + workingDirectory);
के साथ ऐसा करने यह काम नहीं करता, इसके समाप्त होने के साथ बाहर आता है की कोशिश की है है वही कार्य निर्देशिका।
किसी भी मदद की सराहना की जाएगी, यह बहुत उपयोगी जानना होगा।
अधिक विशिष्ट होने के लिए, मैं ऊपर दिए गए स्निपेट में जावा में गतिशील रूप से बनाई गई प्रक्रिया की कार्यशील निर्देशिका ढूंढ रहा हूं। यह महत्वपूर्ण है क्योंकि उपरोक्त पूर्वनिर्धारित आदेश, कार्यशील निर्देशिकाएं कभी-कभी बदल सकती हैं, मैं बाद में उपयोग के लिए स्मृति में परिवर्तनों को सहेजना चाहता हूं।
मैं यह करने के लिए एक रास्ता मिल गया है, और यह समस्या मुक्त काम करने के लिए
यहाँ कैसे मैं भेजे कार्यशील निर्देशिका
public int osType = 1; // This is for Windows (0 is for Linux)
public boolean isValidPath(String path) {
try {
Paths.get(new File(path).getAbsolutePath());
} catch (InvalidPathException | NullPointerException ex) {
return false;
}
return true;
}
public String tracePath(String path) {
try {
if (!path.contains("%%") && !isValidPath(path)) return null;
if (path.contains("%%")) path = path.substring(path.indexOf("%%"));
int lastIndex = -1;
char filesystemSlash = ' ';
if (osType == 0)
filesystemSlash = '/';
if (osType == 1)
filesystemSlash = '\\';
if (osType == 0)
path = path.substring(path.indexOf(filesystemSlash));
if (osType == 1)
path = path.substring(path.indexOf(filesystemSlash) - 2);
String tmp = path;
boolean broken = true;
while (!isValidPath(tmp)) {
int index = tmp.lastIndexOf(filesystemSlash);
if (lastIndex == index) {
broken = false;
break;
}
tmp = tmp.substring(0, index);
lastIndex = index;
}
if (broken && lastIndex != -1) {
tmp = path.substring(0, lastIndex);
}
return tmp;
} catch (StringIndexOutOfBoundsException ex) {
return null;
}
}
यहाँ से निपटने रहा है लगता है की अनदेखी करने का तरीका है पथ के साथ समस्याएं (इसका उपयोग न करके)
public boolean setDirectory(ProcessBuilder pb, String path) {
try {
pb.directory(new File(new File(path).getAbsolutePath()));
return true;
} catch (Exception ex) {
return false;
}
}
अब यहां है कि मैं कैसे हूं विंडोज या लिनक्स के लिए प्रक्रिया शुरू करने
File file = null;
if (osType == 1) {
ProcessBuilder pb = new ProcessBuilder(new String[] { "cmd", "/c", command + " & echo %% & cd" });
pb.redirectErrorStream(true);
if (!workingDirectory.equals(""))
setDirectory(pb, workingDirectory);
process = pb.start();
} else if (osType == 0) {
file = new File("script.sh");
FileWriter writer = new FileWriter(file, false);
writer.append(command + " && echo %% && pwd");
writer.flush();
writer.close();
ProcessBuilder pb = new ProcessBuilder(new String[] { "bash", System.getProperty("user.dir") + "/script.sh" });
pb.redirectErrorStream(true);
if (!workingDirectory.equals(""))
setDirectory(pb, workingDirectory);
process = pb.start();
} else
return;
अंत में यहाँ पाश है कि इस प्रक्रिया का प्रबंधन और कार्यशील निर्देशिका
while (process.isAlive() || process.getInputStream().available() > 0) {
byte[] returnBytes = new byte[1024];
process.getInputStream().read(returnBytes);
char[] arr = new String(returnBytes).trim().toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (Character.isDefined(c))
sb.append(c);
}
String response = sb.toString();
if (!response.equals("")) {
String path = tracePath(response.trim().replace("\n", "").replace("\r", ""));
if (path != null && osType == 1) {
if (Paths.get(path).toFile().exists())
workingDirectory = path;
} else if (path != null && osType == 0) {
if (Paths.get(path).toFile().exists())
workingDirectory = path;
}
client.sendMessage(response + '\r' + '\n');
}
}
if (file != null) file.delete();
यहाँ है प्राप्त आदेश से उत्पादन वेबसाइट
Connecting..
Connected.
Success. You have been connected -> Speentie
bash -c pwd
/root/hardsceneServer/remoteServer
%%
/root/hardsceneServer/remoteServer
bash -c cd ..
%%
/root/hardsceneServer
bash -c pwd
/root/hardsceneServer
%%
/root/hardsceneServer
bash -c dir
ircServer nohup.out remoteServer start.sh start1.sh start2.sh
%%
/root/hardsceneServer
bash -c cd ircServer
%%
/root/hardsceneServer/ircServer
bash -c dir
HardScene.jar hardscene_banned.properties start.sh
hardscene.properties nohup.out
%%
/root/hardsceneServer/ircServer
क्या आदेश यहाँ है? –
यदि आप पूछ रहे हैं कि किसी अन्य प्रक्रिया की कार्य निर्देशिका को कैसे पुनर्प्राप्त करें, तो यह संभव नहीं है। – Andreas
लिनक्स ऐसा कर सकता है जब यह संभव नहीं है? मैंने सोचा कि जावा मशीन के साथ इंटरफ़ेस भी कर सकता है? एक रास्ता होना चाहिए, भले ही यह बेहद जटिल हो। – Speentie8081