Solved

How can I import a class into another app

Posted on 2008-10-15
8
1,083 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
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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 step-by-step guide to install VisualVM launcher in eclipse.

705 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

13 Experts available now in Live!

Get 1:1 Help Now