• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • 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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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