Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1106
  • Last Modified:

How can I import a class into another app

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
studioEtc
Asked:
studioEtc
4 Solutions
 
chaitu chaituCommented:
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
 
sciuriwareCommented:
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
 
ManishLeadCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
studioEtcAuthor Commented:
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
 
sciuriwareCommented:
>>> 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
 
Phil_CrusaderCommented:
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
 
muktajindalCommented:
>>> 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
 
studioEtcAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now