Steve W
Steve W

Reputation: 1128

Running Java file with 2 args using perl script launched in Cygwin

I'm trying to write a Perl script to launch my Java program.

I'm trying to execute it using Cygwin in Windows 10.

The Java program's main method requires two arguments.

The main method looks like this

public static void main(String[] args) throws InterruptedException, IOException
{
    String ipAddress = args[0];
    int port = Integer.parseInt(args[1]);
    ACPCGame acpc1 = new ACPCGame();
    acpc1.play(ipAddress, port);
}

I then have my Perl script called canarybot.pl.

my @args = ("java", "-jar", "canarybot.jar", "localhost", "55001");
system(@args);

localhost and 55001 are supposed to be the two arguments expected by the Java main method.

I then open a Cygwin bash prompt in the directory containing the Perl script and jar file and attempt to execute it like this

./canarybot.pl

This results in the error

./canarybot.pl: line 1: syntax error near unexpected token '('
./canarybot.pl: line 1: my @args = ("java", "-jar", "canarybot.jar", "localhost', "55001");

Upvotes: 0

Views: 306

Answers (2)

Steve W
Steve W

Reputation: 1128

This was resolved by editing the Perl script to use a shebang line along with an exact path to the java.exe file as follows:

#!/usr/bin/perl
my @args = ("/cygdrive/c/Program\ Files/Java/jdk1.8.0_152/bin/java.exe", "-jar", "canarybot.jar", "localhost", "55001");
my $ret = system(@args);

Upvotes: 0

Borodin
Borodin

Reputation: 126722

The error syntax error near unexpected token is a shell error. You are asking the Cygwin shell to execute a Perl program, and of course it makes nonsense in that context

You may use

    perl canarybot.pl

or you can add a shebang line

#!perl

to the start of your program, which will cause the Cygwin shell to look for perl on the PATH and use it to execute the subsequent code instead of trying to interpret it itself

But I don't see any reason to use Cygwin here. From the Windows cmd prompt you can use the command

canarybot.pl

Windows will start searching in the current working directory for the file, and the it will use the registry to determine how to process the .pl file type. There is no need for a shebang line at all

Upvotes: 3

Related Questions