Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

string to string array

Posted on 2011-03-19
29
314 Views
Last Modified: 2012-05-11
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
Comment
Question by:STEVE00098
  • 14
  • 14
29 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35171958
Do you see test.class
when you do dir test.* from the same command line (assuming you are on Windows)
0
 

Author Comment

by:STEVE00098
ID: 35171960
yes sir I do\ see test.class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35171961
You have to have the main method
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:STEVE00098
ID: 35171970
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35171975
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35171981
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35171988
And in the start line you want to separate them by semicolons

java test "mysting;which;is;split"

0
 

Author Comment

by:STEVE00098
ID: 35172015
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
 

Author Comment

by:STEVE00098
ID: 35172020
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172027
Post your code exactly as you have it and I'll try.

0
 

Author Comment

by:STEVE00098
ID: 35172030
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172041

Well, it works for me.

Are you sure you recompiled the program with javac before running it?
0
 

Author Comment

by:STEVE00098
ID: 35172053
let me try again, will keep you posted
0
 

Author Comment

by:STEVE00098
ID: 35172057
deleted test.class and then recompiled, but still same error
0
 

Author Comment

by:STEVE00098
ID: 35172059
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35172066


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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172072
Aare you running it from DOS command lime on Windows ?
0
 

Author Comment

by:STEVE00098
ID: 35172076
yes
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35172079
Any "package..." on top ?
0
 

Author Comment

by:STEVE00098
ID: 35172080
no
0
 

Author Comment

by:STEVE00098
ID: 35172084
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172093
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
 
LVL 27

Expert Comment

by:rrz
ID: 35172101
In what directory is the file  test.class   located ?
0
 

Author Comment

by:STEVE00098
ID: 35172132
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
 

Author Comment

by:STEVE00098
ID: 35172144
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172147

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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172150
Yes, that is right,  array will just have 4 elements, Strings:  "1", "2","3", "4"
0
 

Author Comment

by:STEVE00098
ID: 35172164
Thank you very much for the help
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35172182
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question