Solved

string to string array

Posted on 2011-03-20
16
220 Views
Last Modified: 2012-05-11
The below code works find w/o any issues, I want to add few .jar files to the below class , how would I achieve it? Do I need to create a packagae and project to add jar files? .

Thank  you

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
Comment
Question by:STEVE00098
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35175381
@STEVE00098

try to use any one IDE then you can work easy !!

for me best IDE is Eclipse http://www.eclipse.org/downloads/
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35175568
No you don't need to add packages.
Just put your jars in the same folder where you are working and
add them to CLASSPATH in such way:

Let suppose your jar files are called a1.jar, b1.jar and c1.jar

Then before yoy run compliation you set the CLASSPATH:

set CLASSPATH=.\;.\a1.jar;.\b1.jar;.\c1.jar

After you specify such CLASSPATH you can use the classes
in your jars in your code.
Howvere for that you'll need to know the package structure
inside your jars.
Say if in your jar you have class MyClass within a pcakage com.mycompany,
then on top of you code where you use it you'll have
have to say

import com.mycompany.MyClass;

To find this path under Widnows is easy - copy
your a1.jar to a1.zip (better into some separate folder).
And open a1.zip with your winzip
You'll see ther directory structure and you'll see
folder path com/mycompany/ and in there MyClass
so you'll know after that to add the package as
specified above to your code.

It is true that ide, say Eclipse, will help you with most of that
and you'll eventually end up using it.
Hoiweer if you start learning java - such initial
exercise will add to the understandiong of what you are doing.
I think it is a good idea to try it this way.







0
 
LVL 47

Expert Comment

by:for_yan
ID: 35175588

Below I corrected and expanded a little bit the wording of my post above -
you don't need to read the above message - rad only below:


No you don't need to add packages.
Just put your jars in the same folder where you are working and
add them to CLASSPATH in such way:

Let suppose your jar files are called a1.jar, b1.jar and c1.jar

Then before yoy run compliation you set the CLASSPATH:

set CLASSPATH=.\;.\a1.jar;.\b1.jar;.\c1.jar

After you specify such CLASSPATH you can use the classes
in your jars in your code.
Howvere for that you'll need to know the package structure
inside your jars.
Say if in your jar you have class MyClass within a package com.mycompany,
then on top of you code where you use it you'll have
have to say

import com.mycompany.MyClass;

To find this path under Windows is easy - copy
your a1.jar to a1.zip (better into some separate folder, where you do not work).
And open a1.zip with your winzip
You'll see there the folder structure and you'll see
folder path com/mycompany/ and in there file called MyClass.class
so you'll know after that to add the class within the package in the import line as
specified above to your code.
After you added that import line you can use MyClass in your code;
say you can say something like

 MyClass mc = new MyClass();
mc.executeSomeMethod();
etc.
 

It is true that IDE, say Eclipse, will help you and save tome  with most of that
and you'll eventually end up using one of the IDE's
However if you are starting learning java - such initial
exercise will add to the understandiong of what you are doing.
I think it is a good idea to try it in this way first.


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35176281
>> I want to add few .jar files to the below class

For what purpose?
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 35180009
why you need to add jar files? its a stand alone program that that can be compiled and used with out any jar in the classpath during compilation as well as during runtime environment.
0
 

Author Comment

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

\Java\jdk1.6.0_24\bin

C:\Program Files\Java\jdk1.6.0_24\bin>javac test.java

C:\Program Files\Java\jdk1.6.0_24\bin>java test.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class
Caused by: java.lang.ClassNotFoundException: test.class
        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.class.  Program will exit.

without main can't we run java application from command window
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35182240
Without main you can't run java application in any way, as main is the starting point of the run
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35182262
woevwere your error here is that you type .class
you should type
 java test
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 35182271
And when you type
 java test
it will report the error that your test.class does not have main method
0
 

Author Comment

by:STEVE00098
ID: 35182417
is it mandatory to have a main method is that right sir?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35182444
Yes, in the Application it is mandatory to have the
public static void main(String [] args)
method.
It should be called main - and have the above signature - always.
That's how java virtual machine will knkoow hwre to start
0
 

Author Comment

by:STEVE00098
ID: 35182613
So I can't create a jar file out of the below code?

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35182731
You can yes, Check out

http://technojeeves.com/tech/test.jar 

Run as below
java -jar test.jar 'one;two;three'

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35182737
you can create a .jar file
and use it in your application together with your other classes, one of which should have
main method - and that one you should name in your java command
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35182743
Sample main method:


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

    public static void main(String[] args) {	
	System.out.println(java.util.Arrays.toString(test.toArray(args[0])));
    }

}

Open in new window

0
 

Author Closing Comment

by:STEVE00098
ID: 35182803
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 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.

920 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

14 Experts available now in Live!

Get 1:1 Help Now