• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileInputStream/FIleOutputStream - Copy File  RSS feed

 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill
Now i got it..

Jesper de Jong - I have not noticed it, yes i wrote it by mistake. thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!