首页 > 解决方案 > 从 perl 脚本调用 jar 文件(java 文件)

问题描述

我正在使用 eclipse,我需要从 perl 脚本调用一个 jar 文件。

#!"C:\xampp\perl\bin\perl.exe"
print "Content-Type: text/html\n\n";
my @args = ("java", "-jar", "C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar");
system(@args);

这是我在 perl 文件(echo.pl)中用来调用 jar 文件的代码,谁能告诉我这个“C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0 .1-SNAPSHOT.jar" 这是 jar 文件所在的路径。

标签: javaperljar

解决方案


OP 的代码是完美的双引号误用案例,use strict并且use warnings会警告潜在的问题

use strict;
use warnings;
use feature 'say';

print "Content-Type: text/html\n\n";
my @args = ("java", "-jar", "C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar");

say for @args;

输出

Unrecognized escape \R passed through at misuse_double_quote_1.pl line 6.
Unrecognized escape \A passed through at misuse_double_quote_1.pl line 6.
Unrecognized escape \A passed through at misuse_double_quote_1.pl line 6.
Content-Type: text/html

java
-jar
C:SERSRAJENDRAPRASADHCLIPSEWORKSPACEAPPLICATIONPROTECTOR       ARGETAPPLICATIONPROTECTOR-0.0.1-SNAPSHOT.JAR

Perl 解释器通过扩展反斜杠序列执行双引号字符串的插值

正确的代码@args = ('...','...','...')

use strict;
use warnings;
use feature 'say';

print "Content-Type: text/html\n\n";
my @args = ('java', '-jar', 'C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar');
say for @args;

输出

Content-Type: text/html

java
-jar
C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar

更自然的方法是将代码编写为

use strict;
use warnings;
use feature 'say';

say "Content-Type: text/html\n";
my @args = qw/java -jar C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar/;

say for @args;

system(@args);

输出

Content-Type: text/html

java
-jar
C:\Users\RajendraPrasadH\eclipseworkspace\ApplicationProtector\target\ApplicationProtector-0.0.1-SNAPSHOT.jar

推荐阅读