Solved

string to string array

Posted on 2011-03-20
16
224 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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