java


Java main method not recognized in jar file, but compiles fine


The first thing I'd like to point out is that I don't use IDEs. I use Notepad++ which has minimal assistance and I compile and run all my code from bash or DOS. In this case, I'm attempting to run a file in Java as an executable jar file using the command java -jar CharDefs.jar but it always gives me the message: Main method not found in LispDefs.CharDefs, please define the main method as: public static void main(String[] args).
So far, I have gone through the tutorials on Oracle's site and created a manifest file by making a text file called Manifest.txt with the line Main-Class: LispDefs.CharDefs and a carriage return.
I included this into my jar file with the command jar cfm LispDefs.jar Manifest.txt LispDefs/*.class as indicated on their tutorial.
The file created successfully, but it still gives me the message. However, when I compile the file without the package header and run it with java.exe alone, then it runs successfully. Here is my code:
//package LispDefs;
public class CharDefs {
public static void main(String[] args) {
System.out.println(lChar("hello"));
System.out.println(rChar("hello"));
}
public static String cChar(String var1, String var2) {
return var1.concat(var2);
}
public static String lChar(String var1) {
if (var1.length() == 0) {
return new String();
}
if (var1.length() == 1) {
return String.valueOf(var1.charAt(0));
}
return String.valueOf(var1.charAt(0));
}
public static String rChar(String var1) {
if (var1.length() == 0) {
return new String();
}
if (var1.length() == 1) {
return new String();
}
String temp = new String();
for (int k = 1; k < var1.length(); k++) {
temp = temp.concat(String.valueOf(var1.charAt(k)));
}
return temp;
}
}
What I'm trying to do is create a package of different classes that allow different layers of LISP style list parsing methods that can be applied generically across different programs. Right now, I'm just trying to test running it by itself, but it's not recognizing my main method in the jar executable. I read other articles and tried various things and it seems like maybe there's some sort of semantic error or something I didn't notice perhaps. I'm not too used to Java, as I'm sort of porting this concept over from python.
It looks like you misread, or glossed over some details of, how an "executable jar" is made.
First of all, you have a source directory in which you have your source files (files whose names end up with .java, with an appropriate directory structure; I understand that you know that much).
What you need to do is compile those files in your source directory(ies) into a destination directory (THIS MAY BE THE SAME); and in addition, in one of your source directories, you should have a file named (relatively):
META-INF/MANIFEST.MF
And in that file, have a line which reads:
Main-Class: relative.path.to.MyMainClass
where:
relative/path/to/MyMainClass.java
has a main method with the prototype:
public static void main(String... args) // or String[] args
So, the basic process is:
know what your source directory is, where your .java files are;
know what your destination directory is;
issue the compile process appropriately; manually copy files not handled by the compile process (this includes META-INF/MANIFEST.MF) in the destination directory;
make a jar out of the destination directory.
All in all, the contents of your build directory should be:
META-INF/MANIFEST.MF (with the Main-Class in it, at least);
**/*.class (your compiled Java files).
And you should be able to make an "executable jar" out of it.
Of course, if you have external dependencies, the problem is more complicated than that...
Probable your Manifest.txt file does not end with a new line.
Another way to solve your problem is created JAR with manifest using that command:
jar cfe jar-file main-class input-file(s)
Then Manifest file will be created automatically. So, in your case, this command can look like this:
jar cfe LipsDefs.jar CharDefs *
or if you have package:
jar cfe LipsDefs.jar LispDefs.CharDefs LispDefs/*
More information you can find here https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Related Links

Is there a native implementation version of Java Advanced imaging api?
How to speed up servlet that calls 2 web-services?
Spring 3 - What's the standard way of sending #RequestParam values back to the view?
Android custom progress bar component
How reusable Android libraries operate (development, deployment etc)
stuck on first simple android app
java: search file according to its name in directory and subdirectories
Strange exception
java.net.UnknownHostException - intermittant issues
JAX-WS Question
Java file IO and “access denied” errors
Creating an interface in Android
JSON in jsp file only accepts return values from my MVC controller that are null.
App Engine Java: syntax for setting query limit and start offset.
How to use arbitrary data to select an array for R class in android?
GWT listbox - how to look up item index using text?

Categories

HOME
multithreading
client
netsuite
ngrx
google-play
spagobi
q
google-oauth
tizen-web-app
ojdbc
mouse
append
pheatmap
applepay
medical
facebook-php-sdk
imacros
numeral.js
u-sql
modelica
flask-wtforms
worldwind
libtiff
text-rendering
samoa
jquery-ajaxq
css-animations
kvc
kudan
mmap
cultureinfo
microsoft-r
hammerspoon
crystal-reports-2010
nhapi
rst2pdf
instant-messaging
sql-server-2012-express
nameservers
iframe-resizer
winrt-xaml-toolkit
grails-3.1
wallpaper
y86
bytecode-manipulation
qwerty
multilingual
libvpx
lumberjack
reportingservices-2005
node-gyp
import-from-excel
executenonquery
qwt
user-accounts
taffy
hls.js
yii2-extension
drupal-6
forever
crypt
np-complete
media-player
color-profile
chord-diagram
jquery-filter
setuptools
ableton-live
underscore.js-templating
asp.net-4.5
pcf
transmitfile
django-debug-toolbar
xml-attribute
energy
jfugue
iad
spim
content-length
kcachegrind
method-parameters
appfabric-cache
processmodel
web-controls
unity5.2.3
lustre
gadt
gulp-less
doskey
srs
.aspxauth
odata4j
jplaton
interface-orientation
spring-validator
enterprisedb
wsdl-2.0
venn-diagram
windows-phone-7.1.1
jquery-ui-layout
infobox
self-extracting
winbugs14
xsdobjectgen
modelstate
rescale
brewmp
private-members
post-build

Resources

Encrypt Message