Hot questions for Using Azure in eclipse

Question:

I have written up some code to create a file when the code is running. My plan was to put this on Microsoft Azure function app on a recurring schedule, so it runs every hour or day. I will be using a timertrigger and have an HTTP request trigger in there too for testing, however, I do not know where to put in my own code to get it up and running as well as external java libraries.

The base code that is given when creating the function app work and runs with maven, but when I had created another function and put my own code in it would crash and say there were many errors with the external java libraries I used, JSON, JSON simple, and Apache Commons.

The end result I'm looking for is to be able to put my code in and have the program run on a schedule and create the file I want. For now, I am getting errors wherever I insert my code.

UPDATE: I think the issue might be with my external libraries that I am using, but I do not know why they would be causing an issue or how I would fix it. I need the external libraries to make my code work. I added them to the projects build path so they should work fine. It is giving me errors "package org.apache.commons.io does not exist" and so on for all the imported libraries


Answer:

You could follow the guide provided by @Caiyi,or follow some detailed steps I did in the past.

For example, I want to use Azure blob storage sdk in my Java function app.

Function Class:

package com.fabrikam.functions;

import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.ExecutionContext;

import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;

/**
 * Hello function with HTTP Trigger.
 */
public class Function {

    // Configure the connection-string with your values
    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
                    "AccountName=***;" +
                    "AccountKey=***";

    @FunctionName("hello")
    public String hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,
                        ExecutionContext context) {

        try {
            // Retrieve storage account from connection-string.
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

            // Get a reference to a container.
            // The container name must be lower case
            CloudBlobContainer container = blobClient.getContainerReference(req);

            // Create the container if it does not exist.
            container.createIfNotExists();

            return String.format("Hello, I get container name : %s!", container.getName());

        } catch (Exception e) {
            // Output the stack trace.
            e.printStackTrace();
            return "Access Error!";
        }
    }
}

Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fabrikam.functions</groupId>
    <artifactId>fabrikam-functions</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Azure Java Functions</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <functionAppName>fabrikam-functions-20171017112209094</functionAppName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-functions-java-core</artifactId>
            <version>1.0.0-beta-1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>6.0.0</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <groupId>com.microsoft.azure</groupId>
                    <artifactId>azure-functions-maven-plugin</artifactId>
                    <version>0.1.4</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>java-functions-group</resourceGroup>
                    <appName>${functionAppName}</appName>
                    <region>westus2</region>
                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>beta</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}</directory>
                                    <includes>
                                        <include>host.json</include>
                                        <include>local.settings.json</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

</project>

Then use command mvn clean package to pack your maven project into a jar package.

use command mvn azure-functions:run to run your Azure function locally.

Now,if you run the Azure function and you could see the below issue possibly.

java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount

Exception:
Stack: java.lang.reflect.InvocationTargetException
[10/25/2017 2:48:44 AM]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[10/25/2017 2:48:44 AM]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[10/25/2017 2:48:44 AM]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[10/25/2017 2:48:44 AM]         at java.lang.reflect.Method.invoke(Method.java:498)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:19)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:34)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:40)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.handler.MessageHandler.handle(MessageHandler.java:41)
[10/25/2017 2:48:44 AM]         at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:84)
[10/25/2017 2:48:44 AM]         at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
[10/25/2017 2:48:44 AM]         at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
[10/25/2017 2:48:44 AM]         at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
[10/25/2017 2:48:44 AM]         at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
[10/25/2017 2:48:44 AM]         at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
[10/25/2017 2:48:44 AM] Caused by: java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount
[10/25/2017 2:48:44 AM]         at com.fabrikam.functions.Function.hello(Function.java:26)
[10/25/2017 2:48:44 AM]         ... 16 more
[10/25/2017 2:48:44 AM] Caused by: java.lang.ClassNotFoundException: com.microsoft.azure.storage.CloudStorageAccount
[10/25/2017 2:48:44 AM]         at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[10/25/2017 2:48:44 AM]         at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[10/25/2017 2:48:44 AM]         at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[10/25/2017 2:48:44 AM]         ... 17 more
[10/25/2017 2:48:44 AM] .
[10/25/2017 2:48:44 AM]   Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '3450abda-99a0-4d75-add2-a7bc48a0cb51'
[10/25/2017 2:48:44 AM] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result:

It because that the jar packaged without dependent jar packages.

So , please added the snippet of configuration as below into my pom.xml

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>Your main class path</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Then use command mvn-clean-package and you will see two jar files generated.

One is that it does not contain dependent jar packages, and the second one contains dependent jar packages.

Move the fabrikam-functions-1.0-SNAPSHOT-jar-with-dependencies jar into the path:${project.basedir}/target/azure-functions/${function-app-name}/

For me ,it looks like E:\TestAzureFunction\fabrikam-functions\target\azure-functions\fabrikam-functions-20171017112209094.

Don't forget rename the jar to fabrikam-functions-1.0-SNAPSHOT.

Finally, run the azure function successfully and get the output result via the url: http://localhost:7071/api/hello.

In addition, you could refer to this github doc for more configuration details about azure function maven plugin.

Question:

I was following this link to install the plugin for azure in eclipse, but azure tool kit is not showing in available software's. Please see the screenshot below I have checked my proxy settings. Everything seems to be correct. I have also tried to access the URL http://dl.microsoft.com/eclipse from browser, but its giving me the message You do not have permission to view this directory or page. Then I tried eclipse marketplace. That is also not working.I found below errors in eclipse error log

 The following solutions are not available: Azure Toolkit for Eclipse 3.0.3 (id=com.microsoft.azuretools.sdk.feature.feature.group, com.microsoft.applicationinsights.feature.feature.group, com.microsoft.azuretools.hdinsight.feature.feature.group, com.microsoft.azuretools.core.feature.feature.group, com.microsoft.sqljdbc4.feature.feature.group, com.microsoft.azuretools.docker.feature.feature.group, com.microsoft.azuretools.webapp.feature.feature.group, com.microsoft.azuretools.azureexplorer.feature.feature.group, site=http://dl.microsoft.com/eclipse)

Can anyone please tell why the toolkit is not listing over the available software's.


Answer:

Based on error when you visited marketplace it seems you are using an older version of Eclipse. Azure toolkit is available for Mars or later versions of Eclipse which be required in the section Prerequisites and as the figure below from here.

So please first update your Eclipse, then install Azure Toolkit plugin via Eclipse Marketplace.

Question:

I am under corporate firewall hence got the eclipse neon downloaded with the Azure plugin from the concerned team. Now when trying to log-in to Azure account from eclipse it says

"Sign In Error" and in error details it says "login.microsoftonline.com".

Can anyone help if there is some Issues with proxy or anything in eclipse? I am able to login to portal.azure.com using the same credentials but not from eclipse. Please help.


Answer:

In Eclipse a proxy can be set/configured in Window > Preferences: General > Network Connections.

Question:

We are working with Azure and have run into an issue that hopefully some of you have experience with. I was unable to find any clear documentation on what to do in the following case.

  1. We are deploying a Java application on Tomcat in Azure Web Apps.
  2. Web application uses a 3rd party payment toolkit
  3. 3rd party payment toolkit requires external file
  4. 3rd party payment toolkit needs absolute path to the external file.

My question is:

  1. Where can external files be placed in Azure environment ?
  2. How can I find the absolute path to the external config file ?

There is no other way around it as the 3rd party library requires absolute path currently, as such putting the file in the CLASSPATH does not seem to work. I need to put the external file in the Azure environment somewhere and then be able to reference it with an absolute path so that the library can find it.

I am currently deploying the project from Eclipse into Azure directly.

Please let me know if any of you have any suggestions.


Answer:

Your Java application lives in d:\home\site\wwwroot\webapps\ROOT\, or something very close to that. Use Kudu's DebugConsole to quickly find out the exact path you're deploying to:

Place the configuration file under the same path (but make sure it's not directly accessible from the web) or better yet d:\home\site\some_directory.

d:\home\site is world readable/writeable.

Question:

I am trying out a Digital Signature project with Microsoft Azure blob storage. I built the project using Eclipse Neon and it is executing perfectly.

I want to execute java-callgraph for the same project so that I get to know the run time executions/interactions with Azure.

The terminal command that I used is :

java -Xbootclasspath:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/pallavi/.m2/repository/com/microsoft/azure/azure-storage/4.0.0/azure-storage-4.0.0.jar:/home/pallavi/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.0/jackson-core-2.6.0.jar:/home/pallavi/.m2/repository/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar:/home/pallavi/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar:/home/pallavi/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/home/pallavi/.m2/repository/com/code/flexiprovider/1.7/flexiprovider-1.7.jar:/home/pallavi/.m2/repository/com/code/CoDec/21/CoDec-21.jar:/home/pallavi/Experiment/Azure/target/Azure-0.0.1-SNAPSHOT.jar -javaagent:/home/pallavi/git/java-callgraph/target/javacg-0.1-SNAPSHOT-dycg-agent.jar -jar /home/pallavi/Experiment/Azure/target/Azure-0.0.1-SNAPSHOT.jar

I also tried:

java -Xbootclasspath:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/pallavi/Experiment/Azure/target/Azure-0.0.1-SNAPSHOT.jar -javaagent:/home/pallavi/git/java-callgraph/target/javacg-0.1-SNAPSHOT-dycg-agent.jar -classpath /home/pallavi/.m2/repository/com/microsoft/azure/azure-storage/4.0.0/azure-storage-4.0.0.jar:/home/pallavi/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.0/jackson-core-2.6.0.jar:/home/pallavi/.m2/repository/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar:/home/pallavi/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar:/home/pallavi/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/home/pallavi/.m2/repository/com/code/flexiprovider/1.7/flexiprovider-1.7.jar:/home/pallavi/.m2/repository/com/code/CoDec/21/CoDec-21.jar -jar /home/pallavi/Experiment/Azure/target/Azure-0.0.1-SNAPSHOT.jar

And I get NoClassDef error for

javax/crypto/BadPaddingException

, while the project runs perfectly with eclipse IDE itself.

I tried the solutions to the same issue posted and the error still pertains. This is a maven project and the pom.xml has all dependencies.

Can someone please help me resolving this or suggest a way that java-callgraph can be executed via eclipse IDE to serve the same purpose?


Answer:

According to your description, per my experience, I think the referenced library jce.jar missed in your command which be include in the path jre/lib/.

Meanwhile, I suggest that you can refer to the eclipse help page to export a runnable jar file which packaged all referenced libraries to reduce the terminal command length.

Hope it helps. Any concern, please feel free to let me know.