string to string array

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

}
STEVE00098Asked:
Who is Participating?
 
for_yanCommented:
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
 
Sathish David Kumar NArchitectCommented:
@STEVE00098

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

for me best IDE is Eclipse http://www.eclipse.org/downloads/
0
 
for_yanCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
for_yanCommented:

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
 
CEHJCommented:
>> I want to add few .jar files to the below class

For what purpose?
0
 
msk_apkCommented:
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
 
STEVE00098Author Commented:
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
 
for_yanCommented:
Without main you can't run java application in any way, as main is the starting point of the run
0
 
for_yanCommented:
woevwere your error here is that you type .class
you should type
 java test
0
 
for_yanCommented:
And when you type
 java test
it will report the error that your test.class does not have main method
0
 
STEVE00098Author Commented:
is it mandatory to have a main method is that right sir?
0
 
for_yanCommented:
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
 
STEVE00098Author Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
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
 
STEVE00098Author Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.