[Logo]
Forums Register Login
FileInputStream/FIleOutputStream - Copy File


When I run this code
C:\ankit>java CopyFile.java NewCopyFile.java
It will create a new file named "NewCopyFile" and Copy the whole data.

Now when i run this code
C:\ankit>java CopyFile.class NewCopyFile.class
It will run successfully but when i type
c:\ankit>java NewCopyFile
It gives an error..
Exception in thread "main" java.lang.NoClassDefFoundError: NewCopyFIle (wrong na
me: CopyFile)

Is it not possible to copy the class file? Why?
please Explain

Thanks in Advance
The error message is pretty clear: you ran "java NewCopyFile", and the JVM tried to load the class "NewCopyFile" out of the file "NewCopyFile.class", but it found that the file actually contains a class named "CopyFile". Since you asked it to run NewCopyFile, and it can't, it reports the error and stops.

In other words, your program copies class files just fine, but class files are required to be named after the class they contain.
(1 like)
 

ankit maini wrote:
When I run this code
C:\ankit>java CopyFile.java NewCopyFile.java
It will create a new file named "NewCopyFile" and Copy the whole data.

Now when i run this code
C:\ankit>java CopyFile.class NewCopyFile.class
It will run successfully but when i type ...


Are you sure? Because those commands are both wrong ways to start a Java program.

To compile a Java source file, type:

javac SomeClass.java

Note that it's javac and you must give the name of the source file, including the extension .java.

To run a compiled Java program, type:

java SomeClass arg0 arg1 arg2 ...

Note that you give the name of the class here - not the filename, so you don't specify the extension .class (and certainly not the source file with the .java extension).

The commands that you mentioned:

java CopyFile.java NewCopyFile.java
java CopyFile.class NewCopyFile.class

are both invalid and will give you error messages.

You need to be precise when dealing with computers, because the computer understands you only when you give it a command that is exactly correct.
Thanks Ernest Friedman-Hill
Now i got it..

Jesper de Jong - I have not noticed it, yes i wrote it by mistake. thanks
Fire me boy! Cool, soothing, shameless self promotion:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature


This thread has been viewed 4634 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 26, 2018 03:43:32.