Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the following program but whenever I try to run it I get an error that it

ID: 3913373 • Letter: I

Question

I have the following program but whenever I try to run it I get an error that it cannot find or load the main class. Can you please take a look at it and tell me what to add to the comment area or tell me what is wrong with that causes the run to fail? Thank you.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class CopyFileContent {

public static void main(String[] args) {

/* Comment Here */

File sourceFile = new File("file1.txt");

/* Comment Here */

File destFile = new File("file2.txt");

/* Comment Here */

if (!destFile.exists()) {

try {

destFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

InputStream input = null;

OutputStream output = null;

try {

/* Comment Here */

input = new FileInputStream(sourceFile);

/* Comment Here */

output = new FileOutputStream(destFile);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = input.read(buf)) > 0) {

output.write(buf, 0, bytesRead);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally {

try {

if (null != input) {

input.close();

}

if (null != output) {

output.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

Explanation / Answer

Possible Reason of fail :

1) File name should be same as Class Name. Please Name the file as CopyFileContent.java instead of something else.java

2)
The other cause is that the class name is correct, but that the java command cannot find the class.


Let me know if naming it to same name as Class worked for you. If NOT COMMENT AND I WILL MAKE IT WORK


Thanks