Javac does not create Main Class Reference - java

I am trying to package up some java code which has references to the Hadoop API for java. I am writing the code on my PC and moving it to a CentOS VM and then compiling it.
For some reason when I run the javac command and then un-jar the product file my ./META-INF/MANIFEST.MF file does not have a line which defines the main class such as "Main-Class: folder1.folder2.file". I believe this is why I am getting a "no main manifest attribute when trying to run Hadoop.jar".
Just to step through the process I have the code written using netbeans and import the Hadoop API .jar file. After saving I upload Hadoop.java (this is the only file with code other than the Hadoop API) file to the VM in the /usr/dan directory.
From /usr/dan I run javac -classpath /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar -d ./Hadoop ./Hadoop.java there aren't any errors displayed.
I then use jar cvf ./Hadoop.jar -c ./Hadoop/ to create the jar file. There aren't any errors displayed.
I then try to run the jar file with java -jar Hadoop.jar and get the error.
Is the only important file I need to upload to the VM the .java file? I have the Hadoop API jar file in /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar?
Once I execute the javac command the API is included in the .class file and I no longer need /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar, correct? Or does this file need to be retained in a certain path relative to the .jar file?
What are the possible reasons the manifest file would not have a Main method defined in it?
Much appreciation for any help, I've been racking my brain on this all weekend.
EDIT:
By default the manifest include nearly empty, as stated here:http://docs.oracle.com/javase/tutorial/deployment/jar/defman.html. I can easily add an entry to the manifest file with the m flag. I based my usage off of http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html. It seemed to me that this was something which had to be done after the jar file was created, but just adding the m flag with the appropriate components when initially jarring the file will do the trick in one shot.

If you create your jar through the jar command and want additional manifest entries (such as Main-Class), you'd need to use the m option
jar cmf manifest-file jar-file content-files
with a suitable manifest file (which can contain plain manifest entry lines).

In hadoop, it is not necessary that you need to have a Runnable Jar to execute. Hadoop come up with a utility for running Jars. You may use the following command for executing a Jar, which is not Runnable (Main class is missing in ./META-INF/MANIFEST.MF)
hadoop jar Hadoop.jar <PackageName>.<MainClassName> /input /output
If you don't have any packages use the following command
hadoop jar Hadoop.jar <MainClassName> /input /output

Related

My .exe file from launch4j does not work like my java file. Am I missing something?

As someone new to java and bundling programs with code, I was able to successfully get the proper output running a simple new HelloWorld java file. So I believe there are no issues with the java file in terms of compiling it to a class file or running it afterwards, and that I have all the files needed in the java kit to create an executable file. However, I am not sure if I am using launch4j properly to get the .exe, either with setting up the .jar or through the process from making a proper .xml file.
The code below shows what I get when I try to put everything into the .jar file, but I guess the output is an error because a new .jar file isn't produced unless I take out the "m" from the "cvfm" in the jar command. The code does show something about the manifest being added regardless when doing so, yet I still do not get a proper application. After getting the correct output without launch4j, I stopped recreating the .java and .class files and just focused on the .xml when recreating the .jar file achieved no difference. I have tried in launch4j leaving the environment variables blank in the JRE tab or just included the same path of the system variables that the java file worked with correctly in the command prompt, and I have also switched the check of GUI to console in the header tab. Research has also told me to look up a manifest.mf file, to which I don't think I have that precise file in the JDK, but may have found something similar in the kit (at least when looking in typical areas like the bin folder).
C:\JavaTest>jar cvfm HelloWorld.jar HelloWorld.class
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:406)
at java.util.jar.Manifest.read(Manifest.java:234)
at java.util.jar.Manifest.<init>(Manifest.java:81)
at java.util.jar.Manifest.<init>(Manifest.java:73)
at sun.tools.jar.Main.run(Main.java:176)
at sun.tools.jar.Main.main(Main.java:1288)
C:\JavaTest>jar cvf HelloWorld.jar HelloWorld.class
added manifest
adding: HelloWorld.class(in = 426) (out= 289)(deflated 32%)
I always get a warning about signing when testing the wrapper, but I don't think that has been an issue like an actual error. Due to the nature of the numerous combinations, it is hard to keep track of what caused the differences in issues, but it seems that now leaving the JRE tab blank except for having a min JRE version yields the error "no main manifest attribute" right from the wrapper test in launch4j. Having the very end of the system variable path included in the environment variable field does the same thing. Before trying to recreate the .jar, switching the header to console would create a .exe without errors, but either opening the application would either do nothing or put the same "no main manifest attribute" output in the command prompt. Now, I can't even use launch4j to test wrappers that have the header on console even when building them produces no errors (yet the same error happens when opening the .exe). I am just trying to get the .exe produced from launch4j to provide the same output in the command prompt that I get when typing "java HelloWorld" there.
If I am indeed creating the .jar properly and working with launch4j properly, did I just miss the unlisted step of needing some sort of manifest file to work with launch4j? If so, how would I make sure I got it properly? Would it be seen in a bin folder or completely separate from the JDK? Would I need to move it to my JavaTest folder where the java/class/jar/exe files are? Any help is truly appreciated.
The 'm' in jar cvfm stands for manifest, and implies that you will be providing a file as argument which is the manifest. The f stands for: You will specify the file name.
So, HelloWorld.jar is the argument to the f, and HelloWorld.class is the argument to the m. Your class file, obviously, isn't a valid manifest file, hence why the error occurs.
Generally, use a build tool to make jars, such as maven or gradle. You need a manifest in order to have a Main-Class attribute, and you need a Main-Class attribute to create a runnable jar, and you need a runnable jar to launch4j-ify it.
Make a file named MANIFEST.MF. Create it with a plain text editor. It should contain:
Main-Class: com.foo.thisIsAPackage.YourMainClass
and nothing else.
Then:
jar cvfm HelloWorld.jar MANIFEST.MF YourMainClass.class
note that I'm pretty sure you MUST have a package or this is not going to work.

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Program runs fine in IDE but not as Jar file

I have exported a jar file that I want to run the console. The code compiles and runs correctly in eclipse but I am having an issue running it from the console.
To me it looks like the referenced jar's I added via built path in the Eclipse project file and not being added to the export. If that is the case, how do I ensure that they do? If not, what am I doing wrong?
When you export your source code's class files to a jar using eclipse, only the .class files of your source are exported! Hence your exported jar file doesn't contain the referenced jars you mentioned in eclipse! Due to this, the error occurs while executing from command prompt.
Solution:
Take all the jar files required to execute the program, store it in the same directory as you store the exported jar file. Now while executing the java command, provide all the jar file's names in classpath field as following:
java -classpath .;JAR1.jar;JAR2.jar MainClass
Once you do this, your problem should be resolved!
The dependencies need to be on the classpath, i.e. run like this:
java -cp <path_to_jar1>;<path_to_jar2> -jar ScrumTimeCaptureMaintenence.jar
When running from the command line make sure any dependencies are set on the class path by listing them in the -classpath parameter

creating jar file for application which in turn contains external jar file

Hie all ,
I want to create an jar file for a java application, and i am able create the jar file for it properly.i followed the below link to create the jar file .enter link description here
But the problem what i am facing is that in my application i added the external jar's like jna.jar , platform.jar and vlcj-1.2.0-javadoc.jar files to run my application successful. This application will run fine when i run in eclipse , but when i create the far file for the same application I'm getting the error like
No class defination found error.
jna native library missing.
so please will you tell me what is the actual problem and how do I solve it . and one more thing while creating the jar file I enabled the option "Export java source files and resources".
thanks in advance
Datta
You have to supply full class path when you are running your application, i.e. something like:
java -cp myapp.jar;jna.jar;platform.jar com.mycompany.MyMain
If you are using -jar option you should put all third party libraries to manifest.mf packaged into your jar under META-INF, i.e. add line like:
Class-Path: jna.jar platform.jar
to you manifest.
Pay attenition that you should use ; for windows command line, : for unix command line and space when writing class path in manifest.

The dreaded java.lang.NoClassDefFoundError

I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

Categories

Resources