Java Create a folder using ip address - java

I need to create a folder on my remote machine. For testing purpose, I am trying to create folder on my own machine using my machine IP address. My Ubuntu machine IP address is XX.X.X.XX. I want to create a folder called E book on my home directory using the following java code snippet. But the following doesn't create a folder. Please help.
boolean folder = new java.io.File("//XX.X.X.XX/home/EBook").mkdirs();
System.out.println(folder);

You can accomplish this by executing a process in java:
BufferedReader stdError = null;
try
{
// change the you and server accordingly
String command = "ssh you#server \"mkdir /home/name/EBook\"";
Process p = Runtime.getRuntime().exec(command);
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
String s = null;
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (stdError != null)
{
try
{
stdError.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Related

Run python file for AWS CloudFormation using JAVA

I want to run a python file that can run AWS CloudFormation template using JAVA.
I am passing python file in JAVA code.
When I run the JAVA code it pauses at the following state:
compile-single:
run-single:
If i run the Python file from terminal it works perfectly.
Java Code:
private void RunPythonActionPerformed(java.awt.event.ActionEvent evt) {
String pythonScriptPath = "path to python file";
String[] cmd = new String[2];
cmd[0] = "python"; // check version of installed python: python -V
cmd[1] = pythonScriptPath;
// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = null;
try {
pr = rt.exec(cmd);
// retrieve output from python script
} catch (IOException ex) {
Logger.getLogger(Page2.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
try {
while((line = bfr.readLine()) != null) {
// display each output line form python script
System.out.println(line);
}
// TODO add your handling code here:
} catch (IOException ex) {
Logger.getLogger(Page2.class.getName()).log(Level.SEVERE, null, ex);
}
}
Provide path to your source file at <complete path to your python source file>
Copying working code for you. For me output is Python 3.6.5
package com.samples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcessBuilderSample {
public static void main(String [] args) throws IOException {
RunPythonActionPerformed();
}
private static void RunPythonActionPerformed() throws IOException {
String pythonScriptPath = "python -V";
Process p = Runtime.getRuntime().exec(pythonScriptPath);
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
try {
while((line = bfr.readLine()) != null) {
// display each output line form python script
System.out.println(line);
}
// TODO add your handling code here:
} catch (IOException ex) {
}
}
}

Open files using root android

In java, after getting root permissions, how can I open files with it? Can I open them with File class or do I have to use a command?
You will have to use the su command.
I suggest you have a look at how to use su
Sample code to read a file ( I haven't tested it but it should give you an idea ):
public static void runAsRoot(String[] cmds){
try {
Process p = Runtime.getRuntime().exec("su");
BufferedReader reader = new BufferedReader(p.getInputStream());
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line); // add everything to StringBuilder
// here you can have your logic of comparison.
if(line.toString().equals(".")) {
// do something
}
}
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}

Android studio - How to use directly the terminal from java method?

I am new to Android development, and I try to access to the internal terminal (/bin/bash, ...) of Android phone using a java method.
Do you know if such java method exist?
Thanks
You can use Runtime and Process to achieve your task.
private static String executeCommand(String command) {
Process process = null;
BufferedReader reader = null;
String result = "";
try {
String line;
process = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null)
result += line + "\n";
} catch (final Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (process != null)
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
Where command is any available terminal commands like PING 8.8.8.8
I'm not entirely sure as to what you mean by "accessing the internal terminal". But if you would like to execute commands take a look at the documentation of the Runtime class.
Here's an example on how to use it.

How to get all slave nodes running in Hadoop cluster and resource available on it using Java Code

i was searching the way to find all the slave nodes available in Hadoop cluster but not succeeded. So finally what i tried is, i wrote a Java code to read the slaves file present below path
*$HADOOP_HOME/etc/hadoop/slaves* (Am using hadoop-2.2.0),
using Simple Java Code:
BufferedReader br = null;
try {
String slaveNode="";
br = new BufferedReader(new FileReader("/usr/local/hadoop/etc/hadoop/slaves"));
while ((slaveNode = br.readLine()) != null) {
System.out.println(slaveNode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
but i need the method from which i can get the slave nodes present and resource available on it.Please help me here..

Process started from Java hangs

I am trying to execute a c++ code from java on a remote Windows machine. In order to deal with the remote part, I have created a Web service from where the actual command is run using Runtime.exec(). The c++ exe is not being called directly from the java code. I have a batch file that eventually calls the exe.
The problem is, both java and c++ processes hang. The java code on server side does handle the output stream and error stream. Also, the c++ code is logging everything in a file on Windows. The strange thing is that, when I remove the WS call and run the java code on server side as a standalone java program, it succeeds. Here is the java code:
public class RunCPlusPlusExecutable {
public int runExecutable() {
int exitValue = 0;
try {
Process p = null;
Runtime rt = Runtime.getRuntime();
System.out.println("About to execute" + this + rt);
p = rt.exec("c:/temp/execcplusplus.bat");
System.out.println("Process HashCode=" + p.hashCode());
StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
errorHandler.start();
outputHandler.start();
exitValue = p.waitFor();
System.out.println("Exit value : " + exitValue);
if (exitValue == 0)
System.out.println("SUCCESS");
else
System.out.println("FAILURE");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
}
return exitValue;
}
class StreamProcessor extends Thread {
private InputStream is = null;
private String type = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private FileWriter writer = null;
private BufferedWriter out = null;
StreamProcessor(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
writer = new FileWriter("*******path to log file********");
out = new BufferedWriter(writer);
String line = null;
while ((line = br.readLine()) != null) {
Date date = new Date();
out.write("[" + type + "]: " + date + " : " + line);
out.newLine();
}
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (out != null)
out.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Any idea what is causing the problem and how to debug it? Please note that I won't be able to debug the c++ code.
Thanks
Update 1:
Here are some more details...
The WS server is running from some admin user. And I have been running the standalone java program from some other user.
*It seems that the c++ executable is giving referenced memory error while executing from WS call. There are pop-ups citing the error with OK and Cancel buttons. *
Update 2:
The tomcat server where the WS is deployed is running as a Windows NT service. Can that be the cause of the error? If yes, how to resolve this?

Categories