Solved

string to string array

Posted on 2011-03-20
16
219 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

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

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 42
array6 challenfge 6 65
thymeleaf natural templating vs JSP 2 30
eclipse java  build path 6 25
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

10 Experts available now in Live!

Get 1:1 Help Now