I am working on an Editor application where I can compile and run c,cpp and Java file.I am developing this application using java programming language.I am developing it in Eclipse.
I am able to create new files(c,cpp and java) on specific locations and also I am able to save file to different-2 locations.
And for execution I am using following methods.
String compileFileCommand = "javac "+fileName;
Process compile_process = new ProcessBuilder(compileFileCommand).redirectErrorStream(true).start();
compile_process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(compile_process.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}
My problem is that I am not able to compile files from their corresponding locations.
Always giving Exception
java.io.IOException: error=2, No such file or directory
Please tell me how can I compile and run all c,c++ & java files.
Please also give me any other suggestion for my application.
Edited ..
I have used these two methods for compiling and running.On Compiling it creates a class file in case of Java.But all the time I am getting null from InputStreams(both getErrorStream() and getInputStream()).
void compileJavaFile(String fileName)
{
String compileFileCommand = "javac " + fileName;
try
{
System.out.println("Executing Java File");
Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);
String line = "";
BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
while ((line = bri.readLine()) != null)
{
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null)
{
System.out.println(line);
}
bre.close();
compileProcess.waitFor();
System.out.println("Done.");
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runJavaFile(String fileName)
{
String runFileCommand = "java " + fileName.split(".java")[0];
try
{
System.out.println("runFileCommand : " + runFileCommand);
System.out.println("Running Java File");
Process runProcess = Runtime.getRuntime().exec(runFileCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String line = reader.readLine();
System.out.println("line = " + line);
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
And for C and C++ I am using.
void compileCFile(String fileName)
{
String compileFileCommand = "gcc " + fileName;
resultString = "";
try
{
System.out.println("Compiling C File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".c")[0];
try
{
System.out.println("Running C File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void compileCPPFile(String fileName)
{
String compileFileCommand = "g++ " + fileName;
try
{
System.out.println("Compiling CPP File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCPPFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".cpp")[0];
try
{
System.out.println("Running CPP File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
In case of C and C++ it show error like
g++: /media/disk/eclipse/\/UniversalIDE/CPP/firstCPP: No such file or directory
Please give me solution for my problems ..
Please try following,
Process p = Runtime.getRuntime().exec(command);
command is a string you pass. in command you can pass "javac Test.java" to compile your java file & just like that you can use other commands.
replace the line
String runFileCommand = "./" + fileName.split(".c")[0];
with
String runFileCommand = "./a.out";
Related
I am writing an program to select few columns from one csv and write it into another csv.
i am able to select the columns i want to write but i am not able to write it into another csv.
public class ReadingCSV {
public static void main(String[] args) {
String csvFile = "/Users/Desktop/NEW Automation/combined.csv";
String out = "/Users/Desktop/NEW Automation/a.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] mydata = line.split(cvsSplitBy);
//System.out.println("[Client = " + mydata[1] + " , Acct_id=" + mydata[2] + "]");
PrintWriter output = new PrintWriter("out", "UTF-8");
output.println(mydata[0] + cvsSplitBy + mydata[1] + cvsSplitBy + mydata[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
You have to initialize Printwriter outside your while block, and you need to close it in finally.
public static void main(String[] args) {
String csvFile = "/Users/Desktop/NEW Automation/combined.csv";
String out = "/Users/Desktop/NEW Automation/a.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
PrintWriter output = new PrintWriter("out", "UTF-8");
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] mydata = line.split(cvsSplitBy);
//System.out.println("[Client = " + mydata[1] + " , Acct_id=" + mydata[2] + "]");
output.println(mydata[0] + cvsSplitBy + mydata[1] + cvsSplitBy + mydata[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I will also suggest to use opensource project http://opencsv.sourceforge.net/ to parse csv instead of writing your own code.
So Im working of reading a file containing appointments that I wrote to earlier in my code. I want to sift through the text file and find appointments on a certain date and add them to an ArrayList but when the BufferedReader goes through it, it skips ever other line... Heres my code
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] split = read.split(",");
System.out.println(read);
if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
}
You are reading the line twice..
while ((read = in.readLine()) != null) { // here
read = in.readLine(); // and here
You have error here:
while ((read = in.readLine()) != null)
read = in.readLine();
you should keep the read = in.readLine() in the while. and remove the other line.
pl try this
you r using "read = in.readLine())" two times in while loop that why it is skiping the lomes
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
String[] split = read.split(",");
System.out.println(read);
if (split[0].equals(Integer.toString(checkDay)) && split[1].equals(Integer.toString(checkMonth)) && split[2].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
I got a server applicatino in java in my computer and multiples virtual machines with Android OS. I would like execute the following command line in VM:
su am start -n <PackageName>/ <OtherAtribute>
Look what I already have:
The call:
cvb.accessShell(ip, 5555);
cvb.startApp(appPackageName, mainPath);
THe functions accessShell and startApp:
cvb.accessShell(ip, 5555);
cvb.startApp(appPackageName, mainPath);
public void accessShell(String ip, int port) {
String script = "/home/decom/1018119/adt-bundle-linux-x86_64-20131030/sdk/platform-tools/adb -s "
+ ip + ":" + port + " shell";
System.out.println(script);
try {
System.out.println("Get in");
Runtime.getRuntime().exec(script); // stuck here
System.out.println("Get out");
} catch (IOException e) {
System.out.println("Cant connect to shell");
}
}
public void startApp(String appPackageName, String mainPath) {
String script = "su am start -n " + appPackageName + "/" + mainPath;
System.out.println(script);
try {
Runtime.getRuntime().exec(script);
} catch (IOException e) {
System.out.println("Cant connect to shell");
}
}
The problem is (like the comments on code): My code stuck at shell access and when I run the exactly command by terminal, it works ok.
I tried read the output and nothing is printed.
Try this
Process p = Runtime.getRuntime().exec("ls -la");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
so, in your code:
try {
System.out.println("Get in");
Process p = Runtime.getRuntime().exec(script);
System.out.println("Get out");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Cant connect to shell");
}
i m trying to code for sorting strings,taking input from text file.When i m trying to specify a file for this program is gives me FileNotFoundExcetion
i m unable to understand why?
even i tried to get file path by writing code for that,in the screenShoot u can see that path is correct but the program is still giving me ERROR
here is thet Screenshort
https://app.box.com/s/qytu1d9xlm0vcb6atz42
here is my code
public static void main(String[] args) throws FileNotFoundException, IOException {
ArrayList<String> row1 = new ArrayList<>();
FileWriter writer;
try {
String filename = "1.txt";
String finalfile = "";
String workingDir = System.getProperty("user.dir");
String your_os = System.getProperty("os.name").toLowerCase();
if (your_os.indexOf("win") >= 0) {
finalfile = workingDir + "\\" + filename;
} else if (your_os.indexOf("nix") >= 0 || your_os.indexOf("nux") >= 0) {
finalfile = workingDir + "/" + filename;
} else {
finalfile = workingDir + "{others}" + filename;
}
System.out.println("Final filepath : " + finalfile);
File file = new File(finalfile);
if (file.createNewFile()) {
System.out.println("Done");
} else {
System.out.println("File already exists!");
}
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedReader reader = new BufferedReader(new FileReader("finalfile"))) {
String s;
while ((s = reader.readLine()) != null) {
row1.add(s);
}
Collections.sort(row1);
writer = new FileWriter("output.txt");
for (String s1 : row1) {
writer.write(s1 + "\n");
}
reader.close();
writer.close();
} catch (Exception e) {
System.out.print("Error : " + e);
}
}
In
BufferedReader reader = new BufferedReader(new FileReader("finalfile")))
the parameter to the FileReader constructor is hard coded as "finalfile" - you need to use the variable instead:
BufferedReader reader = new BufferedReader(new FileReader(finalfile)))
^^^^^^^^^^^
You also need to move String finalfile = ""; before the first try block, otherwise it is out of scope when you are creating the FileReader.
Also, there is no need to query the operating system and manually set the directory path separator. If you really need to, use File.separator. Otherwise, simply use the forward slash - this is working cross-platform.
It is good to see that you are using try-with-resources - however, you should do it consequently; simply create all required resources in the try statement, and then there is no need to explicitly close them:
try (BufferedReader reader = new BufferedReader(new FileReader(finalfile));
FileWriter writer = new FileWriter("output.txt")) {
...
// reader and writer will be auto-closed
} catch (IOException e) {
System.out.print("Error : " + e);
}
I want to execute a program (System.getenv("appdata") + "ffmpeg"). I also want to be able get a process or something that could get me the consle output. I have tried "cmd /C " + System.getenv("appdata") + "ffmpeg" before and it didn't seem to work. Any help is appreciated!
Here is some code:
Process p = exec(testFFMpeg);
int ex = -1;
try {
ex = p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
}
if(ex == 0){
System.out.println("Normal execution, exit value: " + ex);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
do{
try {
line = br.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
}
}while(line != null);
}else{
System.out.println("Execution exit value: " + ex);
}
}
private static Process exec(String[] cmd){
try {
return Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
}
The exact location of the file is: `System.getenv("appdata") + "\VinVid\" + "ffmpeg.exe".
I figured it out. The output was going to the error stream, not the inputStream.
You are missing path separator, try this
System.getenv("appdata") + "\\ffmpeg"
In your code change this line line = br.readLine(); to line += br.readLine();
This is how you run a windows process in java.
I don't know what output you want. If your asking to print the output of the process you ran, this code helps :
String line;
String pidInfo ="";
Process p =Runtime.getRuntime().exec(System.getenv("appdata") +"ffmpeg.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
pidInfo+=line;
}
input.close();
System.out.println(pidInfo);