Thufir
Thufir

Reputation: 8497

package and run sample application as a JAR with Netbeans

I want to compile and package this sample code:

thufir@dur:~/NetBeansProjects/HelloAsterisk/src$
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$ javac -cp
asterisk-java.jar ExampleCallIn.java
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$ java -cp
asterisk-java.jar org.asteriskjava.fastagi.DefaultAgiServer Apr 20, 2015
12:19:20 PM org.asteriskjava.fastagi.DefaultAgiServer startup INFO:
Listening on *:4573.
^Cthufir@dur:~/NetBeansProjects/HelloAsterisk/src$
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$ ls asterisk-java.jar 
ExampleCallIn.class  ExampleCallIn.java fastagi-mapping.properties
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$ cat ExampleCallIn.java
import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiException;
import org.asteriskjava.fastagi.AgiRequest;
import org.asteriskjava.fastagi.BaseAgiScript;

public class ExampleCallIn extends BaseAgiScript {

     @Override public void service(AgiRequest request, AgiChannel channel)
     throws
AgiException {
         answer();
         exec("Playback", "tt-monkeys");
         hangup();
     }
}
thufir@dur:~/NetBeansProjects/HelloAsterisk/src$

in Netbeans. However, there's no main class. When I build it as a JAR in Netbeans:

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ ll
total 568
drwxrwxr-x 3 thufir thufir   4096 Apr 20 12:57 ./
drwxrwxr-x 6 thufir thufir   4096 Apr 20 12:57 ../
-rw-rw-r-- 1 thufir thufir 562907 Apr 20 12:57 HelloAsterisk.jar
drwxrwxr-x 2 thufir thufir   4096 Apr 20 12:57 lib/
-rw-rw-r-- 1 thufir thufir   1328 Apr 20 12:57 README.TXT
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ ll lib/
total 560
drwxrwxr-x 2 thufir thufir   4096 Apr 20 12:57 ./
drwxrwxr-x 3 thufir thufir   4096 Apr 20 12:57 ../
-rw-rw-r-- 1 thufir thufir 561237 Apr 20 12:57 asterisk-java-1.0.0.jar
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ jar -tf HelloAsterisk.jar 
META-INF/
META-INF/MANIFEST.MF
ExampleCallIn.class
asterisk-java.jar
fastagi-mapping.properties
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ java -jar HelloAsterisk.jar 
Error: Could not find or load main class helloasterisk.HelloAsterisk
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 

with the MANIFEST.MF as so:

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ jar -xf HelloAsterisk.jar 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_75-b13 (Oracle Corporation)
Class-Path: lib/asterisk-java-1.0.0.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: helloasterisk.HelloAsterisk

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 

How do I build and package this sample code so that it packages as a JAR which runs with the java -jar command?

I'm reasonably sure that the manifest needs modification. Or, perhaps, when executing with java -jar there are some CLI options which should be passed?

The preferred solution is to customize the manifest so that I can press the "run" button in the IDE to run the JAR.

---------------------------------------------------------------------------------update

I apologize, above there's a jar in a jar because there's a jar file in the src directory. Removing that jar gives:

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ java -jar HelloAsterisk.jar 
Error: Could not find or load main class helloasterisk.HelloAsterisk
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ ls lib/
asterisk-java-1.0.0.jar
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ jar -tf HelloAsterisk.jar 
META-INF/
META-INF/MANIFEST.MF
ExampleCallIn.class
fastagi-mapping.properties
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 

and

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ jar -xf HelloAsterisk.jar 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 
thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_75-b13 (Oracle Corporation)
Class-Path: lib/asterisk-java-1.0.0.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: helloasterisk.HelloAsterisk

thufir@dur:~/NetBeansProjects/HelloAsterisk/dist$ 

From this organization, what should the manifest look like? There's a jar specified with Class-Path: lib/asterisk-java-1.0.0.jar.

Keep in mind, that when executing the .class file from the CLI, there's no main class!

see also:

http://article.gmane.org/gmane.comp.telephony.pbx.asterisk.java/18

Upvotes: 0

Views: 341

Answers (1)

JB Nizet
JB Nizet

Reputation: 691835

Jar files must not be embedded inside other jar files. That won't work. With the manifest you have, the structure must be

some-folder
    HelloAsterisk.jar 
    lib
       asterisk-java-1.0.0.jar

And then, withing some-folder, you'll be able to run

java -jar HelloAsterisk.jar

(provided the class helloasterisk.HelloAsterisk indeed exists in one of those two jars).

Upvotes: 1

Related Questions