Solved

string to string array

Posted on 2011-03-19
29
309 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now