Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string to string array

Posted on 2011-03-19
29
Medium Priority
?
321 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 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