Solved

How can I import a class into another app

Posted on 2008-10-15
8
1,091 Views
Last Modified: 2013-11-23
So I managed to write and compile MyUtils.class (no function main()).  I ended up with MyUtils.jar.   I can't figure out how to use the MyUtils methods in another application.  Do I use import myUtils?  If so where do I put MyUtils.jar?  Or better yet where would I tell a user to put MyUtils.jar.

Thanks, Jim

ps I'm using netBeans if that makes any difference.
0
Comment
Question by:studioEtc
[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
8 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 125 total points
ID: 22727429
You said you write some class.did that class having any package name? if yes
in project  properties build path you put this jar.

if you want to use this class write another class and import this class.

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22728321
All you have to do is:
- put the file MyUtils.java between the .java files of the other project,
- remove conflicting 'package' statements from your file,
- call the method correctly:
Assuming you have a static method xyz() inside MyUtils:

      MyUtils.xyz();

So, do away with that .jar file, compile all your .java files and (optionally)
build a .jar from all resulting .class files.

;JOOP!
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 125 total points
ID: 22731264
Are you creating web application? then put jar in WEB-INF/lib folder.
If it is simple java application, put the jar in classpath.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:studioEtc
ID: 22731818
I think I led people a bit astray by calling the class MyUtils.  I mistakenly thought it would make my question simpler to understand.  The real class I wrote reads in a comma delimited file, does proprietary calculations on each row, and writes the results to a new file.  We need to deliver this to our client so that he can incorporate it into his code.  It's like supplying a DLL.  (I also need to be sure that or proprietary calculations remain hidden and cannot be decompiled etc -- But I'll make a new post for that question).

I've been able to test by including a main class in the package.  Now I would like to test it in the same way that the client would use it.

I'm completely new to Java and don't want the client to think I'm a complete idiot.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22732193
>>> cannot be decompiled etc
Forget about it, everything can be hacked, but why would you suspect?

>>> Now I would like to test it in the same way that the client would use it.
Then call the method(s) inside that class from your 'main'.

>>> and don't want the client to think I'm a complete idiot.
You would be if you did not ask for help; there is a lot of bad programming
in the world already.
But, give us the info we need to help you.

Now why would it work 'inside' a 'main' class and not in a separate class?

;JOOP!
0
 

Assisted Solution

by:Phil_Crusader
Phil_Crusader earned 125 total points
ID: 22732552
You mentioned you'll be giving the client a jar file?  

If the client will be using your class in their own Java application, they should include your class in their classpath.  If they will be using it in a J2EE application they should put it in their WEB-INF/lib folder.

Either way, you will have to give them some documentation on how to use your class, ex:
      Full Package & Class Name:  MyUtils.MyUtils
      List of Public variables & functions and how to use them.

On another note, if your providing them a GUI application, why not just include a batch file that runs your code and make it simpler for the client.  You can just zip the files and give them that.




0
 
LVL 5

Assisted Solution

by:muktajindal
muktajindal earned 125 total points
ID: 22737925
>>> cannot be decompiled etc
You may use an obfuscator while creating your jar.

>>> Now I would like to test it in the same way that the client would use it
- Create a new project.
- Include your jar in this project classpath.
- Import MyUtils in your test class in the project. If your package structure defined in MyUtils is something like com.utils, then you will need to give the import statement in your test class as follows :
import com.utils.MyUtils;
- For static methods in MyUtils, you may directly use them by calling MyUtils.method()
- For non-static methods, you will need to do the following :
  MyUtils myUtils = new MyUtils();
  myUtils.method();
0
 

Author Closing Comment

by:studioEtc
ID: 31506575
I really hate trying to figure out how to split points and be fair.  Being new to java, it was my lack of understanding of -classpath that was giving me grief. Several people tried one way or another to point out that -classpath was was the issue. So I split the points between the responses that mentioned classpath.  
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

636 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