• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

FileInputStream/FIleOutputStream - Copy File

 
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
    Bookmark Topic Watch Topic
  • New Topic