Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Importing Third Party Libraries

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Importing Third Party Libraries

    Am trying to complete the Java tutorial on Block Chain at "https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa" and am having problems getting the import to work in MyEclipse. Regardless of whether I follow the method shown in the tutorial or imprt it the way I used to do I get aan error on the import line in the file NoobChain.java. Where am I going wrong?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    The jar files containing the classes being imported must be on the classpath when the javac command is executed so the compiler can find the definitions for the classes.
    Do some research on how to configure Eclipse to put a jar file on the classpath.

    If you were to compile in a command prompt window The command line would be something like this:
    javac -cp .;<Path to jar file> TheClassname.java
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    Found 3 different ways to put a jar file in the class path.
    First , there is the method from the tutorial which places the jar file in a directory with an arbitrary name which is on the same level as the JRE System Library
    Second, do not create a User Library just add the jar to the build path which installs it in a directory call Referenced Libraries
    Third, create a directory at project root called lib and place the jar in it and open it (did not open)
    All of these methods failed to eliminate the the import error
    Have used the last two before with no problems

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    Is the spelling of the path used on the import statement correct? Java is case sensitive.

    Try using the commandline statement I gave earlier to see if the javac command finds the class when given the path to the jar file on the command line.

    I get an error on the import
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Moving to IDE section.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    Quote Originally Posted by Norm View Post
    Is the spelling of the path used on the import statement correct? Java is case sensitive.
    Try using the commandline statement I gave earlier to see if the javac command finds the class when given the path to the jar file on the command line.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Moving to IDE section.
    I did try the commandline you suggested, tried it in a cmd window and a terminal out of MyEclipse. In both I was told that 'javac' is not recognized as an internal or external command,
    operable program or batch file. So I check the system variables and found that contrary to the before Oracle JDK instilation does not include setting path and JAVA_HOME. I set them and restarted the cmd window and the MyEclipse terminal. The system indicates the path and JAVA_HOME values were correct but javac could not be found. The terminal showed no change in the variables and javac could not be found.
    In the tutorial step before adding gson to the project it compiled and ran with the expected results, not sure how it could do that.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    'javac' is not recognized as an internal or external command,
    To help the OS find a command, add the path to the folder containing the command to the PATH environment variable.
    The system indicates the path and JAVA_HOME values were correct
    How does it do that? The proof of correctness is when the OS finds the command on the path.


    You can also give the full path to the command in the command prompt window:
    C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose TestCode25.java

    Also:
    I get an error on the import
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    Quote Originally Posted by Norm View Post
    To help the OS find a command, add the path to the folder containing the command to the PATH environment variable.
    How does it do that? The proof of correctness is when the OS finds the command on the path.
    You can also give the full path to the command in the command prompt window:
    C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose TestCode25.java
    Also:
    Please copy the full text of the error message and paste it here. It has important info about the error.
    I don't remember having to put all of the files in jdk bin directory in the path in order to use them. Expect JAVA_HOME to help with that. (https://www.cs.unm.edu/~ralovato/cs2...h-windows.html)

    I does it because I when I open a comand console and type "echo %path%" it prints out
    "C:\Users\JAJones>echo %path%"
    "C:\Program Files\Java\jdk1.8.0_201\bin;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Windows\system32;C:\Windows;C:\Windows\ System32\Wbem;C:\Windows\System32\WindowsPowerShel l\v1.0\;C:\Windows\System32\OpenSSH\;C:\Windows\sy stem32\config\systemprofile\AppData\Local\Microsof t\WindowsApps;C:\Users\JAJones\AppData\Local\Micro soft\WindowsApps;"
    When I open an Eclipse console and type "echo %path%" I get:
    C:\Users\JAJones>echo %path%
    C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Windows\system32;C:\Windows;C:\Windows\ System32\Wbem;C:\Windows\System32\WindowsPowerShel l\v1.0\;C:\Windows\System32\OpenSSH\;C:\Windows\sy stem32\config\systemprofile\AppData\Local\Microsof t\WindowsApps;C:\Users\JAJones\AppData\Local\Micro soft\WindowsApps;
    BTW JAVA_HOME is set to "C:\Program Files\Java\jdk 1.8.0_201\bin"

    When I enter: "C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe -cp . -Xlint -Xdiags:verbose NoobChain.java" from "C:\Users\JAJones\Workspaces\BlockChain\noobch ain" (project root) I get "The filename, directory name, or volume label syntax is incorrect". (command console)
    Tried issuing the command in the src directory and using "src\NoobChain.java"

    Seems my system is hopelessly messed up

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    When I enter: "C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe -cp . -Xlint -Xdiags:verbose NoobChain.java" from "C:\Users\JAJones\Workspaces\BlockChain\noobch ain" (project root) I get "The filename, directory name, or volume label syntax is incorrect". (command console)
    Tried issuing the command in the src directory and using "src\NoobChain.java"
    Note: Only the command should be in "s not the options and filename:
    "C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe" -cp . -Xlint -Xdiags:verbose NoobChain.java


    Please copy the full contents of the command prompt window and paste it here, so we can see what is happening.

    For example:
    C:\>javac thereIsNoSource.java
    javac: file not found: thereIsNoSource.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    C:\>
    JAVA_HOME is not used by the OS so setting it doesn't help the OS find the commands in the JDK's bin folder.


    Also you need to post the full text of the compiler's error message
    I get an error on the import
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    C:\Users\JAJones\Workspaces\BlockChain\noobchain\s rc\noobchain>javac NoobChain.java
    NoobChain.java:4: error: package com.google.gson does not exist
    import com.google.gson.GsonBuilder;
    ^
    NoobChain.java:8: error: cannot find symbol
    public static ArrayList<Block> blockchain = new ArrayList<Block>();
    ^
    symbol: class Block
    location: class NoobChain
    NoobChain.java:8: error: cannot find symbol
    public static ArrayList<Block> blockchain = new ArrayList<Block>();
    ^
    symbol: class Block
    location: class NoobChain
    NoobChain.java:12: error: cannot find symbol
    blockchain.add(new Block("Hi im the first block", "0"));
    ^
    symbol: class Block
    location: class NoobChain
    NoobChain.java:13: error: cannot find symbol
    blockchain.add(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash));
    ^
    symbol: class Block
    location: class NoobChain
    NoobChain.java:14: error: cannot find symbol
    blockchain.add(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
    ^
    symbol: class Block
    location: class NoobChain
    NoobChain.java:16: error: cannot find symbol
    String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson( blockchain);

    ^
    symbol: class GsonBuilder
    location: class NoobChain
    7 errors

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    error: package com.google.gson does not exist
    Is there a jar file with that contains that package? Use any zip file utility to view the contents of the jar file. If needed, rename the jar file by adding .zip to the end of the filename so that a zip utility can easily be used.

    Is that jar file on the classpath when the javac command is executed?

    What is the command line used to compile the java source?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    javac NoobChain.java

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    That should work if:
    the folder with the javac.exe file is in the PATH
    the NoobChain.java file is in the current folder
    there are no third-party classes used in the java source file

    Can you copy the full contents of the command prompt window and paste it here when that command is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    C:\Users\JAJones\Workspaces\BlockChain\noobchain\s rc>javac noobchain\NoobChain.java
    noobchain\NoobChain.java:4: error: package com.google.gson does not exist
    import com.google.gson.GsonBuilder;
    ^
    noobchain\NoobChain.java:16: error: cannot find symbol
    String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson( blockchain);

    ^
    symbol: class GsonBuilder
    location: class NoobChain
    2 errors

    The jar file is in the Referenced Libraries directory which shows up in MyEclipse but not in the cmd console, Why?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    not in the cmd console,
    Is that inside of the Eclipse program? I don't use Eclipse and don't know how to configure it.
    That was the reason I suggested the use of the javac command with the -cp option in a command prompt window completely outside of Eclipse.

    If the command:
    javac -cp .;<Path to jar file> NoobChain.java
    worked, then you would know that the source code and jar file were correctly connected.

    Can you try that command in a command prompt window open in the folder with the source file and then copy the full contents of the command prompt window and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    C:\Users\JAJones\Workspaces\BlockChain\noobchain\s rc>javac -cp .;C:\Users\JAJones\Downloads\Java_Libraries\gson-2.6.2.jar noobchain\NoobChain.java
    it compiled

    --- Update ---

    Quote Originally Posted by Norm View Post
    Is that inside of the Eclipse program? I don't use Eclipse and don't know how to configure it.
    That was the reason I suggested the use of the javac command with the -cp option in a command prompt window completely outside of Eclipse.

    If the command:
    javac -cp .;<Path to jar file> NoobChain.java
    worked, then you would know that the source code and jar file were correctly connected.

    Can you try that command in a command prompt window open in the folder with the source file and then copy the full contents of the command prompt window and paste it here?
    cmd console is wwhat used to be called the dos prompt run cmd

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    it compiled
    Ok, that would say the jar file and the source file are fine.

    Now you need to figure out how to make it work in Eclipse.

    noobchain\NoobChain.java
    Why do you have a folder name with the source filename?
    Is there a noobchain folder inside the C:\Users\JAJones\Workspaces\BlockChain\noobchain\s rc folder?

    What happens when you use the javac command in the
    C:\Users\JAJones\Workspaces\BlockChain\noobchain\s rc\noobchain
    folder?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    yes it is the package

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Importing Third Party Libraries

    Ok, now you need to figure how to configure Eclipse to use the jar file.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing Third Party Libraries

    For the sake of later readers this is what I got from the MyEclipse support
    Your project has module-info.java file which is introduced from Java 9 onwards.
    Please read these for more details about modules :
    https://www.oracle.com/corporate/fea...9-modules.html
    Module Directives in Java 9 - codeNuclear

    If there is a module-info.java file in the project, then you need to add the jar to the Modulepath section in the Libraries tab instead of Classpath and then declare the module dependencies in the file. For this case of gson jar, this is how the module-info.java file should look :

    1
    2
    3
    module noobchain {
    requires gson;
    }
    Please make the changes accordingly and let us know how it works.

    –Swapna
    Genuitec Support[COLOR="Silver"]

Similar Threads

  1. Party Bits problem
    By frodooftheshire in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 10:49 AM
  2. control third party page in java.
    By kirti in forum Member Introductions
    Replies: 1
    Last Post: May 2nd, 2010, 03:33 PM
  3. How to get data from a third party website
    By Duff in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 04:24 PM
  4. [SOLVED] Using 3rd Party JAR Files
    By oss_2008 in forum Java Theory & Questions
    Replies: 12
    Last Post: June 12th, 2009, 10:27 AM