about classpath in run configuration in eclipse - java

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();
}
}
}

Related

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) {}
}
}
}

How to write the output to console and file?

I have to output the method sayHello() twice.
The output should be written to the console, and once in a file.
I wrote some code but I don't get ahead.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Utility {
public static void main(String[] args){
try(OutputStream src = new FileOutputStream("C:/Users/baum/Documents/TestText.txt");
InputStream dest = new FileInputStream("C:/Users/baum/Documents/TestText.txt")){
sayHello(src, dest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sayHello(OutputStream src, InputStream dest)throws IOException{
String t = "Hello World!!!";
OutputStreamWriter out = new OutputStreamWriter(src);
InputStreamReader in = new InputStreamReader(dest);
out.write(t.toCharArray());
out.flush();
in.close();
}
}
try {
for (String line : Files.readAllLines(Paths.get("C:\\path\\to\\text.txt"))) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
That's maybe a better way

Java write to .csv file

I am trying to write to a .csv file, but I keep getting the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable writeToFile
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
The error is associated with the line:
void writeToFile(String Filename){
Here is my code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;
public class writeFileExample {
public static void main(String[] args) {
void writeToFile(String Filename){
double steps=0;
File file=new File(Filename);
file.createNewFile();
FileWriter writer=new FileWriter(file);
try {
//Integrate integrate=new Integrate();
//for (steps=10;steps<1000000;steps=steps*10){
//double area_value=integrate.integrate_function(steps);
writer.write("Steps"+","+"Area");
//}
//System.out.println(area_value);
writer.flush();
writer.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I can't see any syntax errors.
Taking into account Reimeus' comment below I edited it a bit. I now have:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;
void writeToFile(String Filename){
public class writeFileExample {
public static void main(String[] args) {
double steps=0;
etc.
I am getting the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Filename cannot be resolved to a variable
Any help appreciated.
Java doesnt support nested methods. Move writeToFile out of the main method
public class Test {
public static void main(String[] args) {
try {
writeToFile("c:\\abc.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static public void writeToFile(String Filename) throws IOException
{
.
.
.
.
}
}

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

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" ?

Running xjc from java code

I am using xjc to generate classes from xsd. The generation has to happen inside the java code. Right now I have done it like this:
Process child = Runtime.getRuntime().exec(command);
try {
System.out.println("waiting...");
child.waitFor();
System.out.println("waiting ended..");
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
The output for the above program is:
waiting...
I have to use the classes after they are generated. The problem here is that the subprocess never exits and the control is never back to the java program!
Is there a way to do this without getRuntime().exec() ?
You can actually use the driver class (com.sun.tools.xjc.Driver) behind the command line tool. This worked for me:
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Driver;
import com.sun.tools.xjc.XJCListener;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Generator {
public static void main(String[] args) throws BadCommandLineException, IOException {
final String targetDir = "jaxb-files";
Path path = Paths.get(targetDir);
if(!Files.exists(path)) {
Files.createDirectories(path);
}
Driver.run(new String[]{"-d", targetDir,
"D:\\dev\\onepoint\\tui\\java\\xsdjsonschema\\src\\main\\xsd\\test.xsd"}, new XJCListener() {
#Override
public void error(SAXParseException e) {
printError(e, "ERROR");
}
#Override
public void fatalError(SAXParseException e) {
printError(e, "FATAL");
}
#Override
public void warning(SAXParseException e) {
printError(e, "WARN");
}
#Override
public void info(SAXParseException e) {
printError(e, "INFO");
}
private void printError(SAXParseException e, String level) {
System.err.printf("%s: SAX Parse exception", level);
e.printStackTrace();
}
});
}
}
try this
Process child = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(
new InputStreamReader(child.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}

Categories

Resources