0

I would like to replace data in a file with some new data and rename the file with a different name in java. I have the old file saved in the same class directory as the code to carry out the changes is saved. The command that I use in the command prompt is as below.

java ReplacingText oldfile newfile oldstring newstring

I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: ReplacingText (wrong name: replacingtext/ReplacingText)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Could someone shed some light on it.

package replacingtext;

import java.io.*;
import java.util.*;

public class ReplacingText 
{
    public static void main(String[] args) throws Exception
    {

        if (args.length !=4)
        {
            System.out.println(
            "Usage: java ReplaceText sourceFile targetFile oldStr newStr");
            System.exit(0);
        }
        File sourceFile = new File(args[0]);
        if(!sourceFile.exists())
        {
            System.out.println("Source file " + args[0]+" does not exist");
            System.exit(0);
        }
        File targetFile = new File(args[1]);
        if (targetFile.exists())
        {
            System.out.println("Target File " + args[1] + "already exist");
            System.exit(0);
        }
        Scanner input = new Scanner(sourceFile);
        PrintWriter output2 = new PrintWriter(targetFile);

        while (input.hasNext())
        {
            String s1=input.nextLine();
            String s2=s1.replaceAll(args[2],args[3]);
            output2.println(s2);
        }
        input.close();
        output2.close();
    }
}
14
  • And the error you receive is ...? Commented Nov 13, 2013 at 1:39
  • I am using the command java ReplaceText oldfile newfile oldstring newstring Commented Nov 13, 2013 at 1:39
  • 3
    Please add this "garbage" to your post. Commented Nov 13, 2013 at 1:41
  • 1
    Exception in thread "main" java.lang.NoClassDefFoundError: ReplacingText (wrong name: replacingtext/ReplacingText) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) Commented Nov 13, 2013 at 1:50
  • 1
    You've given your class a package declaration, which means you'll run it a bit differently. Suppose you've just compiled your java file and it's located at C:\projects\replacingtext\ReplacingText.class, you must cd to C:\projects and type the following: java replacingtext.ReplacingText oldfile newfile oldstring newstring Commented Nov 13, 2013 at 2:29

2 Answers 2

1

i found it on the net. first, i make a direcoty,name as "replacingtext" ,the package name. then, i move the complied class "ReplacingText.class" into it. last, i run "java replacingtext.ReplacingText "c:/s.txt" "c:/t.txt" haha yes" in "replacingtext" parent directory.

bingo...it works..

but.. i don't konw why.. perhaps the classLoader find the class by the relative path ,not just the class name...

Sign up to request clarification or add additional context in comments.

1 Comment

"the classLoader find the class by the relative path ,not just the class name..." As you say: bingo. You've got it. Here's the reference: docs.oracle.com/javase/tutorial/java/package/managingfiles.html
0

first,i tested the method like this.

public static void main(String[] args) throws Exception {

        String sourceFile = "c://s.txt";
        String targetFile = "c://t.txt";
        String oldS = "haha";
        String newS = "yes";
        test(new String[]{sourceFile,targetFile,oldS,newS});

        //      test(args);
    }

    private static void test(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        if (args.length != 4) {
            System.out
                    .println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
            System.exit(0);
        }
        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(0);
        }
        File targetFile = new File(args[1]);
        if (targetFile.exists()) {
            System.out.println("Target File " + args[1] + " already exist");
            System.exit(0);
        }
        Scanner input = new Scanner(sourceFile);
        PrintWriter output2 = new PrintWriter(targetFile);

        while (input.hasNext()) {
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2], args[3]);
            output2.println(s2);
        }
        input.close();
        output2.close();
    }

it works.

then, i tested based on the method above like this.

public static void main(String[] args) throws Exception {
        test(args);
}

java ReplacingText c://s.txt c://t.txt haha yes

it worked ,too..

ps:cause i didn't config the JAVA_HOME,so I run it in my jdk/bin directory.

2 Comments

You removed package replacingtext; from the OP's code and that allowed you to run it the way you described.
i found it. first, i make a direcoty,name as "replacingtext" ,the package name. then, i move the complied class "ReplacingText.class" into it. last, i run "java replacingtext.ReplacingText "c:/s.txt" "c:/t.txt" haha yes" in "replacingtext" parent directory. bingo...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.