• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Java Runtime Error

Hi,

I Know very little Java. I got a requirement from my client to copy files from one directory to another. I got the following code from Internet. The code got compiled without any error. When I tried to run I am getting the following runtime exception. Please help me to fix this.

C:\Temp>echo %CLASSPATH%
C:\Program Files\VERITAS\Backup Exec\NT\ECM\bumodule.jar;C:\Program Files\VERITAS\Backup Exec\NT\ECM\LOG4J-CORE.JAR;C:\P
LOG4J.JAR;.

C:\Temp>java CopyDirectory('C:\Temp','C:\Y')
Exception in thread "main" java.lang.NoClassDefFoundError: copydirectory('C:\Temp','C:\Y')

Code:
====

import java.io.*;

public class CopyDirectory{
public static void main(String[] args) throws IOException{
CopyDirectory cd = new CopyDirectory();

String arg1 = args[0];
String arg2 = args[1];
File src = new File(arg1);
File dst = new File(arg2);

cd.copyDirectory(src, dst);

}
public void copyDirectory(File srcPath, File dstPath)
throws IOException{
if (srcPath.isDirectory()){

if (!dstPath.exists()){

dstPath.mkdir();
}

String files[] = srcPath.list();
for(int i = 0; i < files.length; i++){
copyDirectory(new File(srcPath, files),
new File(dstPath, files));

}

}
else{
if(!srcPath.exists()){

System.out.println("File or directory does not exist.");
System.exit(0);

}
else
{
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(dstPath);
// Transfer bytes from in to out
byte[] buf = new byte1024;
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);

}
in.close();
out.close();

}
}
System.out.println("Directory copied.");
}

}

Thanks
Ashok
0
Ashok911
Asked:
Ashok911
  • 2
  • 2
  • 2
  • +2
1 Solution
 
CEHJCommented:
Your command line should be something like


java -classpath "%CLASSPATH%" CopyDirectory x y

Open in new window

0
 
CEHJCommented:
Or in your case


java -classpath "%CLASSPATH%" CopyDirectory C:\Temp C:\Y

Open in new window

0
 
silemoneCommented:
the obvious:  the class isn't being found...

basically you have to call the command from where the program is located...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
silemoneCommented:
or go the more complex route and set path with CEHJ assistance  :0)
0
 
cmalakarCommented:
If you have already set CLASSPATH environment,..
then you can run the java without classpath parameter.

Your arguments should be separated from the class file name.

You can also run..

java CopyDirectory   "C:\Temp"   "C:\Y"
0
 
Ashok911Author Commented:
Many thanks to CEHJ.

Silemone,
  Thanks for your suggestions.
Ashok.
0
 
cmalakarCommented:
Aah.. Too late :(
0
 
objectsCommented:
> java -classpath "%CLASSPATH%" CopyDirectory C:\Temp C:\Y

you don't need to specify the classpath on the command line, thats just unecessary typing
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now