2

I'm trying to write a shell code that will execute my Java program. I tried this:

#!/bin/sh
COMPILECMD="javac server.jar"
RUNCMD="java -jar server.jar 12777"

$COMPILECMD
$RUNCMD

But I got this error:

error: Class names, 'server.jar', are only accepted if annotation processing is explicitly requested 1 error Failed to load Main-Class manifest attribute from server.jar

In the command line I don't use the jar file, I just compile and then execute:

javac server.java 
java server 12777
3
  • 3
    javac compiles Java source files into class files - it doesn't create a Jar. To do that, you need to use the jar command as well (and configure what the main-class is so it can create the appropriate manifest). However, you might like to consider using ant or maven to perform the compilation and jarring up of the class files. Commented Jan 28, 2014 at 5:02
  • 1
    Since you know what works from the command line, why are you doing something different in the shell script? As Greg said, if you want to build a jarfile you can do that, but you need to use the right sequence of commands to do so, and if you want server to be the default main() class of the jar you need to also create a manifest file saying so. Commented Jan 28, 2014 at 5:04
  • 1
    offtopic comment, but "shell code" has a different meaning. :-) What you're writing here is usually called "shell script" or "shell program", though the terms are not that distinct. Commented Jan 28, 2014 at 7:13

2 Answers 2

2

The typical way to build a Java program into a JAR file from the shell is to use the ant utility.

Its home (and documentation) is here: http://ant.apache.org/

Essentially, you'd write (or use a tool to write) a build.xml in your project folder, and then run ant to compile, bundle, et al.

(Disclaimer: I'm not a fan of Ant, but it is, I believe, the most common/popular tool in the Java universe for this task.)


You can also use traditional Unix Makefiles, if you're familiar with them. Some rules like:

     CLASSES= Server.class Supporting.class

     %.class:   %.java
           javac $<

     %.jar:  $(CLASSES) Manifest.mf
           jar cfm $@ Manifest.mf $(CLASSES)

You could also use a straight-ahead shell script:

      javac Server.java
      javac Supporting.java
      jar cfm Server.jar Manifest.mf Server.class Supporting.class

However, maintaining this is liable to be a nightmare if your project grows beyond a few files.


There are other tools available, as well, but these are the most typical ones I know of.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you get wrong understanding. jar is created by jar command not by javac command.

jar command create a jar file (zip file containing all the class inside) javac command just compile and transform .java to .class only

Example compile-command:

javac server.java; jar cvf server.jar server.class

You may find the following link helping. http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.