Java Lambda挑选了shell命令

时间:2018-02-27 19:40:19

标签: java serverless

我是完全不合格的Java程序员(而且这给了我更多的功劳)但是在使用AWS lambda驱动的后端时,我遇到了从命令行运行JAVA应用程序的需求虽然我完全可以在NodeJS容器中执行shell脚本,但我完全不同于JAVA。我一直在寻找有用的指导,但我找不到我正在寻找的东西。希望有人可以给我一个指针:

  1. 使用vs-code作为编辑器,如何将amazonaws运行时库用于我的代码?我有这样的陈述:

      import com.amazonaws.services.lambda.runtime.Context;
      import com.amazonaws.services.lambda.runtime.RequestHandler;
    

    在编译时抱怨包裹不存在。很公平,很确定他们,但我不确定如何包含它们。我知道这是基本的东西。

  2. 我找到了一些代码,这些代码似乎为执行shell命令,然后选择 stdin stdout 提供了一个很好的起点,但当我更换了演示示例时" ps -ef "用" ls "我立即发现抱怨没有设置当前的工作目录。如何设置 cwd ,我可以使用相对引用吗?

    下面是我希望利用的代码片段(我将用已提供的Java可执行文件替换shell命令,并且stdout和stdin将使用Lambda回调而不是{{1}发回在这里完成。

    println
  3. 当我运行演示项目public static void main(String args[]) { String s = null; try { // run the Unix "ps -ef" command // using the Runtime exec method: Process p = Runtime.getRuntime().exec("ps -ef"); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } System.exit(0); } catch (IOException e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); System.exit(-1); } } 时,我从vs-code输出窗口(特别是语言支持工具输出)中收到以下错误:

    <错误 - 上午11:57:10] 2018年2月27日,上午11:57:05初始化失败 无法使用指定的Gradle分发&#39; https://services.gradle.org/distributions/gradle-2.13-bin.zip&#39;创建工具API实施的实例。 org.gradle.tooling.GradleConnectionException:无法使用指定的Gradle分发&#39; https://services.gradle.org/distributions/gradle-2.13-bin.zip&#39;创建Tooling API实现的实例。     在org.gradle.tooling.internal.consumer.loader.DefaultToolingImplementationLoader.create(DefaultToolingImplementationLoader.java:106)     在org.gradle.tooling.internal.consumer.loader.CachingToolingImplementationLoader.create(CachingToolingImplementationLoader.java:44)     在org.gradle.tooling.internal.consumer.loader.SynchronizedToolingImplementationLoader.create(SynchronizedToolingImplementationLoader.java:43)     在org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.onStartAction(LazyConsumerActionExecutor.java:101)     在org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.run(LazyConsumerActionExecutor.java:83)     在org.gradle.tooling.internal.consumer.connection.CancellableConsumerActionExecutor.run(CancellableConsumerActionExecutor.java:45)     在org.gradle.tooling.internal.consumer.connection.ProgressLoggingConsumerActionExecutor.run(ProgressLoggingConsumerActionExecutor.java:58)     在org.gradle.tooling.internal.consumer.connection.RethrowingErrorsConsumerActionExecutor.run(RethrowingErrorsConsumerActionExecutor.java:38)     at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor $ 1 $ 1.run(DefaultAsyncConsumerActionExecutor.java:55)     at org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)     at org.gradle.internal.concurrent.ManagedExecutorImpl $ 1.run(ManagedExecutorImpl.java:46)     在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)     at java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)     at org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)     在java.base / java.lang.Thread.run(Thread.java:844)     在org.gradle.tooling.internal.consumer.BlockingResultHandler.getResult(BlockingResultHandler.java:46)     在org.gradle.tooling.internal.consumer.DefaultModelBuilder.get(DefaultModelBuilder.java:50)     在org.gradle.tooling.internal.consumer.DefaultProjectConnection.getModel(DefaultProjectConnection.java:41)     在org.eclipse.buildship.core.workspace.internal.ConnectionAwareLauncherProxy.newModelBuilder(ConnectionAwareLauncherProxy.java:64)     在org.eclipse.buildship.core.workspace.internal.DefaultModelProvider.fetchModel(DefaultModelProvider.java:68)     在org.eclipse.buildship.core.workspace.internal.DefaultModelProvider.supportsCompositeBuilds(DefaultModelProvider.java:165)     在org.eclipse.buildship.core.workspace.internal.DefaultModelProvider.fetchModels(DefaultModelProvider.java:75)     在org.eclipse.buildship.core.workspace.internal.DefaultModelProvider.fetchEclipseGradleProjects(DefaultModelProvider.java:99)     在org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.startSynchronization(GradleProjectImporter.java:150)     在org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.importDir(GradleProjectImporter.java:97)     at org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.lambda $ 0(GradleProjectImporter.java:89)     at java.base / java.util.ArrayList.forEach(ArrayList.java:1380)     at java.base / java.util.Collections $ UnmodifiableCollection.forEach(Collections.java:1081)     at org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.importToWorkspace(GradleProjectImporter.java:89)     在org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.importProjects(ProjectsManager.java:105)     在org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.access $ 1(ProjectsManager.java:99)     在org.eclipse.jdt.ls.core.internal.managers.ProjectsManager $ 1.run(ProjectsManager.java:93)     在org.eclipse.jdt.internal.core.BatchOperation.executeOperation(BatchOperation.java:38)     在org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:732)     在org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2214)     在org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2241)     在org.eclipse.jdt.core.JavaCore.run(JavaCore.java:5676)     在org.eclipse.jdt.core.JavaCore.run(JavaCore.java:5633)     在org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.initializeProjects(ProjectsManager.java:86)     在org.eclipse.jdt.ls.core.internal.handlers.InitHandler $ 1.runInWorkspace(InitHandler.java:193)     在org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:39)     在org.eclipse.core.internal.jobs.Worker.run(Worker.java:60) 引起:org.gradle.internal.service.ServiceCreationException:无法使用GlobalScopeServices.createFileWatcherFactory()创建FileWatcherFactory类型的服务。     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:741)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryService.create(DefaultServiceRegistry.java:695)     at org.gradle.internal.service.DefaultServiceRegistry $ ManagedObjectProvider.getInstance(DefaultServiceRegistry.java:537)     at org.gradle.internal.service.DefaultServiceRegistry $ SingletonService.get(DefaultServiceRegistry.java:577)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryService.assembleParameters(DefaultServiceRegistry.java:705)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryService.create(DefaultServiceRegistry.java:694)     at org.gradle.internal.service.DefaultServiceRegistry $ ManagedObjectProvider.getInstance(DefaultServiceRegistry.java:537)     at org.gradle.internal.service.DefaultServiceRegistry $ SingletonService.get(DefaultServiceRegistry.java:577)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryService.assembleParameters(DefaultServiceRegistry.java:705)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryService.create(DefaultServiceRegistry.java:694)     at org.gradle.internal.service.DefaultServiceRegistry $ ManagedObjectProvider.getInstance(DefaultServiceRegistry.java:537)     at org.gradle.internal.service.DefaultServiceRegistry $ SingletonService.get(DefaultServiceRegistry.java:577)     在org.gradle.internal.service.DefaultServiceRegistry.doGet(DefaultServiceRegistry.java:393)     在org.gradle.internal.service.DefaultServiceRegistry.get(DefaultServiceRegistry.java:374)     在org.gradle.tooling.internal.provider.DefaultConnection.initializeServices(DefaultConnection.java:83)     在org.gradle.tooling.internal.provider.DefaultConnection.configure(DefaultConnection.java:70)     在org.gradle.tooling.internal.consumer.connection.AbstractPost12ConsumerConnection.configure(AbstractPost12ConsumerConnection.java:37)     在org.gradle.tooling.internal.consumer.loader.DefaultToolingImplementationLoader.createConnection(DefaultToolingImplementationLoader.java:111)     在org.gradle.tooling.internal.consumer.loader.DefaultToolingImplementationLoader.create(DefaultToolingImplementationLoader.java:89)     在org.gradle.tooling.internal.consumer.loader.CachingToolingImplementationLoader.create(CachingToolingImplementationLoader.java:44)     在org.gradle.tooling.internal.consumer.loader.SynchronizedToolingImplementationLoader.create(SynchronizedToolingImplementationLoader.java:43)     在org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.onStartAction(LazyConsumerActionExecutor.java:101)     在org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.run(LazyConsumerActionExecutor.java:83)     在org.gradle.tooling.internal.consumer.connection.CancellableConsumerActionExecutor.run(CancellableConsumerActionExecutor.java:45)     在org.gradle.tooling.internal.consumer.connection.ProgressLoggingConsumerActionExecutor.run(ProgressLoggingConsumerActionExecutor.java:58)     在org.gradle.tooling.internal.consumer.connection.RethrowingErrorsConsumerActionExecutor.run(RethrowingErrorsConsumerActionExecutor.java:38)     at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor $ 1 $ 1.run(DefaultAsyncConsumerActionExecutor.java:55)     at org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)     at org.gradle.internal.concurrent.ManagedExecutorImpl $ 1.run(ManagedExecutorImpl.java:46)     在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)     at java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)     at org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)     在java.base / java.lang.Thread.run(Thread.java:844) 引起:java.lang.IllegalArgumentException:无法确定来自&#39; 9.0.4&#39;的java版本。     在org.gradle.api.JavaVersion.toVersion(JavaVersion.java:64)     在org.gradle.api.JavaVersion.current(JavaVersion.java:73)     在org.gradle.internal.filewatch.DefaultFileWatcherFactory。(DefaultFileWatcherFactory.java:43)     在org.gradle.internal.service.scopes.GlobalScopeServices.createFileWatcherFactory(GlobalScopeServices.java:288)     at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:564)     在org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)     在org.gradle.internal.service.DefaultServiceRegistry.invoke(DefaultServiceRegistry.java:426)     在org.gradle.internal.service.DefaultServiceRegistry.access $ 1200(DefaultServiceRegistry.java:62)     at org.gradle.internal.service.DefaultServiceRegistry $ FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:739)     ......还有32个

0 个答案:

没有答案