Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default Where to place the jar files..?

    I made a program in netbeans that uses a jar file.. that i imported to the libraries in order to make it run... thats ok. the program is running correctly.

    Now i want to run this very program on another computer where netbeans is not installed.. or say i need to run this program on multiple computers and in that case it would be practically infeasible to install netbeans on every computer just for making this program run..

    I mean ..... i am running the file on the target system and it is obviously unable to recognize the functions that i have used from the "jar " file... where should i place the jar file on the target system or set class paths what ever ..in order to make the program run correctly.; plz suggest. thnx.:confused:
    The Quieter you become the more you are able to hear !

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Doesn't matter where you put the jar file as long as it's in the classpath.

    For example, you have two jars, one with your program, one you depend on. Your directory structure looks like this:

    Java Code:
    /myprogram
      myprogram.bat
      myprogram.jar
      lib/
        needed.jar
    and you want myprogram.bat to execute your program. So it can look like this:
    Java Code:
    java -cp ./myprogram.jar:./lib/needed.jar com.myself.MyCoolProgram
    hope that helps

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It's nothing to do with NetBeans. You said that you've added additional JAR files into your project and it's working fine. Cool. Then what you've to do is, build the project. Right click on the project and from the pop-up menu click on build. You comes with message on output window, notifying that the process is successful or fail. If successful it gives you a folder path to the build package. Actually it's in your project folder, check for a folder name dist there. That's the only folder you want. Since you've used additional JAR files in that folder you can see a folder name lib as well as a JAR file with the name of your project.

    If you want to run your application in another PC, you have to bring that complete dist folder there. That's all. But keep in mind you have to set the CLASSPATH in that new PC, either manually or by programatically.

  4. #4
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    1.
    Doesn't matter where you put the jar file as long as it's in the classpath.
    suppose we forget about the package and all. now i have a single class file, say, "first.class"
    and i have a jar file, say, "jest.jar" .....

    I kept the jar file in the drive "E:\jest.jar" and the class file in the directory "D:\first.class" or in the same directory... next i set the class path --- "E:\jest.jar" manually.. Now when i run the class file it is never executed?? why?

    2. Also i need to run the file "first.class" inside the jar package say "fun.jar".... how can i run the file "first.class" from inside the jar file at the command prompt.? Also to make note of is that this jar file contains multiple class files that are independent programs.. so i have not set any class as the main class of the jar file..! plz suggest. i need to run these programs individually...

    3.
    But keep in mind you have to set the CLASSPATH in that new PC, either manually or by programatically.
    Now i am a bit of confused.. since my setting classpaths isn't working.. so what classpath should i exactly set manually and how can i set the classpath programmatically.plz explain.
    The Quieter you become the more you are able to hear !

  5. #5
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    myprogram.bat
    what commands the batch file is supposed to hold?
    The Quieter you become the more you are able to hear !

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Stephen Douglas View Post
    what commands the batch file is supposed to hold?
    Suppose your main.jar is stored in directory X and another jar file support.jar is stored in directoty X/Y (i.e. you only know the path to Y relative to X); you can put a line in the manifest of main.jar like this:

    Java Code:
    Class-Path: Y/support.jar
    All the installer of all your code has to do is install main.jar in X and support.jar in X/Y and you're in business. You don't need .bat files at all, your manifest can handle it.

    kind regards,

    Jos

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Exactly.
    And Netbeans does all that for you, as Eranga says.
    There's no need to faff with Classpaths either, since the Manifest tells Java where to find its dependencies.

  8. #8
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I am still not answered! sori

    suppose we forget about the package and all.



    Also to make note of is that this jar file contains multiple class files that are independent programs.. so i have not set any class as the main class of the jar file..! plz suggest. i need to run these programs individually...

    using cmd !
    The Quieter you become the more you are able to hear !

  9. #9
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I think I can explain my problem more clearly !!:D

    First forget about netbeans and all..........

    Suppose you make a java file from the notepad that import a package say--
    javax.swing.* . you as all would be knowing that this package is packed into a jar file called rt.jar that is locate in C:\Program Files\Java\jdk1.6.0_14\jre\lib at least in my computer.. now when we compile this program from the command prompt as javac x.java or interpret it as java x this program is executed . definitely surely.

    Now consider a scenario......... apart from using the rt.jar i.e, packages inside this jar file i am using another jar file named click-0.18.jar from which i am using the package org.apache.commons.io.FileUtils exactly like we use the traditional class inside the java packages....i think i am clear uptil now and if not let me know...

    now i am telling what exactly i attempted .ok.

    1. I.... tried to compile the program without setting any classpath ... perhaps if needed. Obviously it isn't going to work. thats f9

    2. Now I set the classpath as say -D:\ where the supporting jar file is kept... i again tried to compile the program.. it didn't worked again.

    3. Next i copied this supporting jar file in the very directory--C:\Program Files\Java\jdk1.6.0_14\jre\lib that keeps the obviously rt.jar and then again tried to compile the program.. and i failed again ...

    How the heck can i compile my program...plz suggest.
    Also if you are still not clear with what i meant in above please do ask. I need help desperatly..thnx:)
    The Quieter you become the more you are able to hear !

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    See the first reply to your post by iluxa.
    you need to use the -cp switch, and then list the required jars (and where they are in relation to where you're running this)...if there are several jars then you can stick them in a file and refer to that file instead. But you still need the -cp switch (and don't forget the "." in the list of locations).

    See here.

    ETA: Don't simply stick the jars in the JRE lib. That's Bad Practice.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    1. I.... tried to compile the program without setting any classpath ... perhaps if needed. Obviously it isn't going to work. thats f9
    Yeah, without setting up the classpath you cannot compile any Java class. If you are using an IDE, then no need to worry about that. Because IDE will care of that. If you are looking to do that on Notepad then yes, you have to set it in environment variables.

    Quote Originally Posted by Stephen Douglas View Post
    2. Now I set the classpath as say -D:\ where the supporting jar file is kept... i again tried to compile the program.. it didn't worked again.
    Can you explain a bit more about how you set the CLASSPATH?

    Quote Originally Posted by Stephen Douglas View Post
    3. Next i copied this supporting jar file in the very directory--C:\Program Files\Java\jdk1.6.0_14\jre\lib that keeps the obviously rt.jar and then again tried to compile the program.. and i failed again ...
    You have to set the class path to bin folder. I'm confusing how you set the CLASSPATH. So as I said earlier can you explain a bit more about the CLASSPATH setting

  12. #12
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    In the case when i placed the click-0.18.jar file in the directory C:\Program Files\Java\jdk1.6.0_14\jre\lib and tried to compile my program from inside the C:\Program Files\Java\jdk1.6.0_14\bin directory.. i think in that case i don't need to set any class paths.. as traditionally


    My question is when my program is using the rt.jar without seeking my concerns then in the case when i placed the click-0.18.jar in the above specified directory where the rt.jar file is also placed then why didn't the program compile as traditionally.. or how can i make the file compile.?

    I don't have very good idea of setting classpaths but i m looking for if you people can help me ... in doing so if required.


    ETA: Don't simply stick the jars in the JRE lib. That's Bad Practice.
    Then where should i place it exactly..


    Isolating my need exactly.. i have made a simple java file that uses a supporting jar file.. now where should i place this jar file or perhaps whatever .. i don't know in order to make my program run...
    plz suggest.
    The Quieter you become the more you are able to hear !

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    You have a project...whether that is a sinlge java class or not is irrelevant. A project has a directory structure with a folder for source (based on packaging) and a folder for dependencies (usually called lib).

    You then use javac to compile, with a -cp flag to reference the lib directory.

    To run this you use java with a -cp flag as mentioned above (listing the jars).

    This way each project maintains its own jars, since each project might require different versions of jars.

  14. #14
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    Also I made a note in one of my earlier posts that i have not set the main class of the jar file... because it contains multiple independent programs that are required to be executed independently ...... so what would be my command on cmd such that i am able to run a file say Cd00.java that is packaged inside the jar file :confused:
    The Quieter you become the more you are able to hear !

  15. #15
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I type this command---

    C:\Users\Mild Seven\Desktop\OtherOther\JSection\COMPIL~1>javac -cp "E:\Inventory
    \Compiler Design\dist\lib" Cd00.java


    What error I get exactly is---

    Cd00.java:11: package org.apache.commons.io does not exist
    import org.apache.commons.io.FileUtils;
    ^
    Cd00.java:21: cannot find symbol
    symbol : variable FileUtils
    location: class Cd00
    String str = FileUtils.readFileToString(chooser.getSelectedFile (), "UTF-
    8");
    ^
    Cd00.java:71: cannot find symbol
    symbol : variable FileUtils
    location: class Cd00
    FileUtils.writeStringToFile(chooser.getSelectedFil e(), output_str, "UTF-
    8");
    ^
    3 errors
    The Quieter you become the more you are able to hear !

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Have you read the documentation?

    Just asking, because all this is in there.

    I've already posted a link to the stuff for "java" command, but here's the page for javac.

    When using -cp for a directory containing jar files you need a * at the end.

  17. #17
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    When using -cp for a directory containing jar files you need a * at the end.

    i don't find exaclty what you are saying ....in the link you specified
    I tried this..............
    C:\Users\Mild Seven\Desktop\OtherOther\JSection\COMPIL~1>javac -cp "E:\Inventory\Compiler Design\dist\lib\*.jar" Cd00.ja
    Iam i now correct??if i am then it isn't working.......

    Can you please tell me what would exaclty be the command.
    The Quieter you become the more you are able to hear !

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Did you read my link to javac?
    I'll quote, since clearly your mouse isn't working or something.

    For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar;b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.
    *...not *.jar

  19. #19
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I worked! I am sorry i was actually looking for the command and overlooked the statements.....
    C:\Users\Mild Seven\Desktop\OtherOther\JSection\COMPIL~1>javac -cp "./*" Cd00.java

    But the following error is shown when i try to interpret the command.. even when the directory contains the compiled version of this program....

    C:\Users\Mild Seven\Desktop\OtherOther\JSection\COMPIL~1>java -cp "./*" Cd00
    Exception in thread "main" java.lang.NoClassDefFoundError: //Cd00/java


    plz suggest!
    The Quieter you become the more you are able to hear !

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    If the jars are in the same directory you're in (which I presume by the "./*") I think you can simply use * in the javac command.

    For the java command, you'll also need to specify "." as well. The "./*" simply tells java to get all the jar files from where you are, but in doing that it assumes that's the only places it needs to look to run things. So it's looking for your class (I think, been a bit) in those jars.

    So you need .;*...or something to that effect. Don't need quotes around them either.

Page 1 of 2 12 LastLast

Similar Threads

  1. Edit In place for JSF
    By akira_mz86@yahoo.com in forum New To Java
    Replies: 2
    Last Post: 02-24-2010, 10:35 AM
  2. Where to place jar files
    By rummy in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 01:08 AM
  3. Extract jar and place in other folder
    By kkk in forum Advanced Java
    Replies: 4
    Last Post: 08-31-2009, 09:42 AM
  4. How To Place Three Files In A Folder
    By ramesh.8189 in forum AWT / Swing
    Replies: 4
    Last Post: 02-12-2009, 07:59 AM
  5. how to place the controls >
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 06:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •