0

I haven't used Java in a long while but I am trying to write a simple Java application which utilizes the AWS API.

The part I am stuck at is that I can't exactly remember how to link the API to the Java code. I am not using eclipse and I don't want to, I like writing my code in just sublime text. So I really need someone to explain to me how to link it all together in simple terms.

My file hierarchy is:

  • Project
    • Test.java
    • aws-java-sdk-1.10.5.1.jar
    • aws-java-sdk-1.10.5.1
      • Documentation
      • lib
        • aws-java-sdk-1.10.5.1-javadoc.jar
        • aws-java-sdk-1.10.5.1-sources.jar
        • aws-java-sdk-1.10.5.1.jar
        • aws-java-sdk-flow-build-tools-1.10.5.1.jar
      • samples
      • third-party

I moved aws-java-sdk-1.10.5.1.jar out of the lib folder and put it on the same level of my source code because that's what I thought I needed to do. For source code, Here is a sample of what I have:

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;

public class Test
{
  public static void main(String args[])
  {
    BasicAWSCredentials cruds = new BasicAWSCredentials("<<ACCESS KEY>>", "<<SECRET KEY>>");
    AmazonSNSClient snsClient = new AmazonSNSClient(cruds);

    System.out.println("Hello World!");
  }
}

In my terminal, I compile my code by using the command:

javac Test.java

however that doesn't work for me and I get the error: cannot find symbol error. I manage to get it to compile by using the following command. (But i feel like the top command should work if my hierarchy is correct?):

javac -cp aws-java-sdk-1.10.5.1.jar Test.java  

After I compile the code and get my class file, I try to run my program with the command

java Test

but that throws me the stacktrace of

Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getMethod0(Class.java:2937)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more

I am sure this is an extremely stupid problem and it’s mostly me not being able to remember how to use this correctly. If someone can give me an idiots guide on how to make this work, I would be so thankful to you!

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
srz2
  • 162
  • 1
  • 10
  • Using Maven (or Gradle) to manage your dependencies for your projects will eliminate these types of issues. However, that will require a different folder structure. – JaredHatfield Jul 19 '15 at 01:05

2 Answers2

2

basically the jar has to be in the class path both when you're compiling and you're running.

you've figured the compiling part. for the run part you do something similar:

java -cp aws-java-sdk-1.10.5.1.jar:. Test

The class loader will (hopefully) look in the right place and pick up the classes.

side comment: if you do development without using and IDE, you're going to have a bad time with java doing things that are not trivial. imports and lack of autocomplete will kill your development speed. Recommend giving IntelliJ or Eclipse a chance.

side comment 2: if you're going to do development and put together the final artifacts by hand you're also going to have a rough time managing dependencies. I recommend looking into something like maven (https://maven.apache.org)

Mircea
  • 10,216
  • 2
  • 30
  • 46
  • I thought of this too and when I tried your command, I get the error: **Error: Could not find or load main class Test** – srz2 Jul 18 '15 at 20:56
  • Error: Could not find or load main class Test – srz2 Jul 18 '15 at 20:58
  • java -cp aws-java-sdk-1.10.5.1.jar:Test.jar Test ? – Mircea Jul 18 '15 at 20:59
  • sort of makes sense, you need both jars. other way would be to specify the dir of the jar - or copy jars to special artifacts dir and specify that dir – Mircea Jul 18 '15 at 21:01
  • do you have a Test.jar as a result of the compile? – Mircea Jul 18 '15 at 21:03
  • No, I just get a .class file, i'm not trying to distribute the software yet – srz2 Jul 18 '15 at 21:05
  • hah. okay that makes sense. try packing the class in a jar using the jar command and after that pushing both or doing it as suggested in answer: java -cp aws-java-sdk-1.10.5.1.jar:. Test – Mircea Jul 18 '15 at 21:08
0

Use Maven and set the POM dependencies. For example, if you use a Java IDE, such as IntelliJ, perform these tasks:

  1. In the IntelliJ IDE, choose File, New, Project.
  2. In the New Project dialog box, choose Maven.
  3. Choose Next.
  4. In GroupId, enter aws-project.
  5. In ArtifactId, enter aws-project.
  6. Choose Next.
  7. Choose Finish.

Note: Add the POM file you find in a service-specific folder to the POM file in the project. Then create a package that you find in the examples and you can start adding the Java classes to your project.

THis is so much easier then trying to hunt down JAR files and include them in your class path.

smac2020
  • 9,637
  • 4
  • 24
  • 38