Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

string to string array

Posted on 2011-03-19
29
Medium Priority
?
323 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
Industry Leaders: 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!

 

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 2000 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 28

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

Technology Partners: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

564 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