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

string to string array

Hello Experts,

Yesterday a expert helped me to write a function to convert from  from string to string array. I was testing the code and I ran into error.

public class test {
   public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }
}

When issuing the command "java test" I get the below error message, please advise

C:\Program Files\Java\jdk1.6.0_24\bin>java test
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: test.  Program will exit.
0
STEVE00098
Asked:
STEVE00098
  • 14
  • 14
1 Solution
 
for_yanCommented:
Do you see test.class
when you do dir test.* from the same command line (assuming you are on Windows)
0
 
STEVE00098Author Commented:
yes sir I do\ see test.class
0
 
for_yanCommented:
You have to have the main method
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
STEVE00098Author Commented:
Java program compiled without any issues, where do I need to put the main method

public class test {
   public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }
0
 
for_yanCommented:
Something like that:


public class test {

public static void main (String [] args) {
String [] s0 = toArray(args[0]]);
for(int j=0; j<so.length; j++){System.out.println(s0[j]);
}

}
  public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

}

then you compile javac test.java

then you start

java test "mysting,which,is,split"

0
 
for_yanCommented:
I guess there were some spelling mistakes,
this is better:


public class test {

public static void main (String [] args) {
String [] s0 = toArray(args[0]);
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}

}
  public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

}

Open in new window


0
 
for_yanCommented:
And in the start line you want to separate them by semicolons

java test "mysting;which;is;split"

0
 
STEVE00098Author Commented:
Were you able to run on your end? I still get this message
C:\Program Files\Java\jdk1.6.0_24\bin>java test
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: test.  Program will exit.
0
 
STEVE00098Author Commented:
C:\Program Files\Java\jdk1.6.0_24\bin>java test "mystring;test;split"
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: test.  Program will exit.
0
 
for_yanCommented:
Post your code exactly as you have it and I'll try.

0
 
STEVE00098Author Commented:
public class test {

public static void main (String [] args) {
String [] s0 = toArray(args[0]);
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}

}
  public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

}
0
 
for_yanCommented:

Well, it works for me.

Are you sure you recompiled the program with javac before running it?
0
 
STEVE00098Author Commented:
let me try again, will keep you posted
0
 
STEVE00098Author Commented:
deleted test.class and then recompiled, but still same error
0
 
STEVE00098Author Commented:
C:\Program Files\Java\jdk1.6.0_24\bin>javac test.java

C:\Program Files\Java\jdk1.6.0_24\bin>java test "1;2;3;4"
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: test.  Program will exit.
0
 
for_yanCommented:


That is strange, I just grabbed the code you posted and it runs without problems
Are you sure you don't have any "package..." on top of the code you posted?


public class test {

public static void main (String [] args) {
String [] s0 = toArray(args[0]);
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}

}
  public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

} 

Open in new window


I ran:
java test "mystring;split"

Output :
mystring
split

Open in new window

0
 
for_yanCommented:
Aare you running it from DOS command lime on Windows ?
0
 
STEVE00098Author Commented:
yes
0
 
for_yanCommented:
Any "package..." on top ?
0
 
STEVE00098Author Commented:
no
0
 
STEVE00098Author Commented:
This is all I have in test.java

public class test {

public static void main (String [] args) {
String [] s0 = toArray(args[0]);
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}

}
  public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

}
0
 
for_yanCommented:
Very strange, I tried from DOS command line - it works for me.

Well, try to say in the same DOS window before running it:

set CLASSPATH=.\;

and then

java test "1;2"
0
 
rrzCommented:
In what directory is the file  test.class   located ?
0
 
STEVE00098Author Commented:
I already have classpath=C:\Program Files\Java\jdk1.6.0_24\bin but when i tried the below recommendation it worked.
set CLASSPATH=.\;
and then
java test "1;2"

So do I have to include set CLASSPATH =.\; in java program
0
 
STEVE00098Author Commented:
On more clarification if for example I pass the string "1;2;3;4"
public static String[] toArray(String s) {
      String[] array = s.split(";");
      return array;
   }

the return array will be 1,2,3,4 is that right?

Thank you
0
 
for_yanCommented:

If you don't have any CLASSPATH defined in DOS window (and DOS windw inherits
your general Windows environment) and you compile and run in some default
folder - it should run, but if you have
some classpath defined differently then it would be a problem and you need
to say set CLASSPATH=.\;
0
 
for_yanCommented:
Yes, that is right,  array will just have 4 elements, Strings:  "1", "2","3", "4"
0
 
STEVE00098Author Commented:
Thank you very much for the help
0
 
for_yanCommented:
You are always welcome.
With your original classpath it does not work for me either even though I went to similar bin folder - I'm not sure I understand why.
In any case you should not normally compile and execute your programs in java /bin folder.
And when you start doing it in any other dos window, check - if you have CLASSPATH set up and
then  change it to .\;
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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