Code doesn't run in eclipse but does in command window - java

import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("invoked successfully");
}
}
The problem is that if I run the Test1_Exec in the Eclipse, Test1.txt is not created and no error is reported. But if I type "java Test1" in the command window, Test1.txt is created. Test1_Exec.java and Test1.java are in the same src folder; Test1_Exec.class and Test1.class are in the same bin folder. So what's wrong with the Eclipse? My version of Eclipse is Kepler(20130614-0229).

Put bin folder in your classpath
Process p = run.exec("java -cp path/to/bin Test1");
Currently, java is looking for Test1.class inside your project directory.

Don't you need to give the full path for Test1 in the command?
i.e: "java c:\code\Test1" ?

Related

FileNotFound exception error even thought the file exists in eclipse

While running my java file io program I'm getting FileNotFoundException
I tried changing the directory of the file and most of the other solution mentioned in SO, nothing works.
My code:
package com.HelloWorld;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileWriter w=null;
BufferedWriter bw=null;
try {
String s="welcome";
String b="‪‪D:\\test.txt";
w=new FileWriter(b);
bw=new BufferedWriter(w);
bw.write(s);
bw.flush();
}
catch(IOException e)
{
System.out.println("exception caught"+e);
}
finally{
try {
if(bw!=null)
bw.close();}
catch(Exception e) {
System.out.println("exception caught"+e);
}
try {
if(w!=null)
{
w.close();
System.out.println("success");
}}
catch(Exception e) {
System.out.println("exception caught"+e);
}
}
}
}
I had already created the file in the D drive so the FileWriter overrides the already created file name because of which it did not write to the file

FileOutputStream doesn't show FileNotFoundException

for FileOutputStream, it will throw a FileNotFoundException if the file doesn't exist, but it will create it if it can.
I dont have a Sample.txt in my project root
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
try {
FileOutputStream s= new FileOutputStream("Sample.txt");
} catch (FileNotFoundException e) {
System.out.println("File not Found");
}
}
}
The problem is:
I cannot see the Output of the "File Not Found" from the Terminal. How did it happen?
Thank you
You can set Sample.txt as a File first and check if it exists with .canWrite()
You still have to put a try/catch around FileOutputStream, but it should never go in the catch block.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class test {
public static void main(String[] args) {
File f = new File("Sample.txt");
if (!f.exists()) {
System.out.println("File not Found");
}
else {
try {
FileOutputStream s = new FileOutputStream(f);
} catch (FileNotFoundException e) {}
}
}
}

Java Issue with Runtime Process Execution: Return 1 Always

I am having an issue running a command line string in Java. My code is below. When I extract the exact string I am running and paste it into the terminal it works perfectly. Am I missing something? I've condensed my code into this basic example, but the issue is the same.
import java.io.File;
import java.io.IOException;
public class TestCommandLine {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
Process pr = null;
try {
pr = rt.exec("/Users/comp/Desktop/executable /Users/comp/Desktop/115.png");
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(pr.waitFor());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

java eclipse fails to detect a "bin" located file

I use eclipse and put a File called "example.txt" into the bin-folder where the class files are generated (and into the sub-folder of the package). But still, the program allways prints out the error message i wrote for the case the file is not found.
Main Class:
import java.io.BufferedReader;
public class Main {
public static void main(String[] args){
BufferedReader file = Console.file("example.txt");
...
}
Console Class:
public final class Console {
public static BufferedReader file(String args) {
BufferedReader file = null;
try {
file = new BufferedReader(new FileReader(args));
} catch (FileNotFoundException e) {
println("Error, file not found!");
System.exit(1);
}
return file;
}
}
any ideas?
For the Eclipse project, the current path is the project folder, not the BIN directory, you can use the code below to get the current path so that you will know where to put and how to access the file.
import java.io.File;
import java.io.IOException;
public class MainTest {
public static void main(String[] args)
{
File directory = new File("");
try {
System.out.println(directory.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So in your case, the path you specify should be: ./bin/example.txt

about classpath in run configuration in eclipse

Test1_Exec.java
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java -cp bin Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1_Exec.class and Test1.class are both in the bin folder under JavaTest(project name), and the codes do work. But I want to replace the code "Process p = run.exec("java -cp bin Test1")" with "Process p = run.exec("java Test1")" by adding bin folder( right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders ), then Test1.txt is not created by new codes. So where is the problem ?
To me program seemed unnecessarily complex. Why not below(if you dont have specific requirement)
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
try {
Test1.createFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void createFile()
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories

Resources