Setting a path for the main-class in the manifest file - java

Say for example my manifest.txt file is saved in a parent directory of my bin directory where by classes are saved. Now, I want to be able to refer to my main-class in the manifest header so that I may create my JAR file from the parent directory directly.
So for example:
my manifest.txt is saved in the directory called project so:
-project(parent directory) contains:
bin(this contains my classes that are needed to run the application)
,src(folder)
and manifest.txt
So in a perfect world I would imagine my manifest.txt to look something like this:
Main-Class: bin/mainclass
but for some reason this doesn't work. Any ideas?

The Main-Class attribute must contain the fully qualified name of the main class. That has nothing to do with how your project is laid out. If the class is named Foo and is in package com.bar, the main class is com.bar.Foo.
If the class Foo is in the default package, which is a bad practice, then its fully qualified name is Foo. But the first thing you should do in that case is moving the class to a package.
When you execute java -jar path/to/myjar.jar, from whatever directory, java will inspect the jar, read the manifest file, look for the Main-Class, and load it from the jar.

Related

How to add a class to classpath to not get import errors in Java [duplicate]

I have two classes:
MyApplication Library
The Library has already been compiled into Library.class and the source code is no longer available. I am now trying to compile MyApplication from source. MyApplication depends on the Library. The Library has a package name of org.myCompany. I tried setting my classpath to the following:
set CLASSPATH=C:\java\project\org\myCompany\Library.class;.
which produced the following javac compiler error message:
MyApplication.java:33: cannot find symbol
symbol: class Library
location: class MyApplication
Library theLibrary = new Library();
So I changed my classpath to be:
set CLASSPATH=C:\java\project\;.
which produced the exact same error message.
How do I set my Windows classpath to include the Library.class file? Should it point at the folder contains the org\myCompany subfolders? Or point directly to the class file? Or to the folder containing the class file (even though the class is in a package and belongs in a subfolder)?
I do an echo %CLASSPATH% after my set command and the classpath is being set correctly. I also made an ant build.xml file and encountered the same problem. In fact, ant -verbose confirmed that my classpath is being set correctly.
First of all: the use of the CLASSPATH environment variable is very strongly discouraged. The best thing is for you to forget that it exists. Use the -cp command line switch or similar methods to set the classpath.
Second, the classpath entries each represent a place where the classloader will start looking for .class according to the package hierarchy, i.e. it will look for the class org.myCompany.Library in a subfolder org/myCompany in any of the classpath entries.
Therefore, if
you add a classpath entry C:\java\project\
and there is a class file C:\java\project\org\myCompany\Library.class
which is actually part of a package org.myCompany (capitalization matters here!)
and your MyApplication class has an import org.myCompany.Library;
Then it really should work.
You cannot add a single class in your classpath like this. You have 3 solutions:
add this class in the path of your other compiled classes (respecting the package naming of your directories)
add the root directory of this class in your classpath (in your case "C:\java\project\")
add this single class into a jar and add this jar to the classpath
For your problem, the thrird choice is cleaner: external dependencies normally are packaged into jar files.
If your .class file isn't in jar file, point your classpath to the parent dir where package of class resides, e.g., for class org.myCompany.Library, point your CP to directory containing org/myCompany.
If your .class file included into some jar file, add full path to that jar to your classpath.
If you compiled the class files to a different directory, the classpath needs to point to where the .class file is.
set CLASSPATH=C:\java\project\;
is correct assuming that that the class file is in the same directory as the .java source file.
Is there a problem locating the Library to the same root project where is your MyApplication class
Example, if:
c:/project/org/company/MyApplication.class
Can you locate the Library class into:
C:/project/org/myCompany/Library.class
please notice, that the folders org/myCompany and org/company are located under the same folder c:/project/.
Please notices that this solution works for you if the Library Class is only used by your application.
Edited
Windows command prompt is tedious, after setting the classpath please close and re-open the Command Prompt, so it can see the new classpath's value.
For the classpath to work, you need to have a folder structure which matches the package hierarchy. So if your class is org.myCompany.Library, you must create a nested folder structure of C:\java\project\org\myCompany and place your Library class file in the myCompany folder. Then set the class path to C:\java\project\

Failed to generate JAR file using Intellij

I have created a jar in path xxx/IdeaProjects/xxx/out/artifacts/abc_jar.
When I run it using java -jar, I get
Could not find or load main class ...
I have moved the mainfest file to xxx/IdeaProjects/xxx/src/main/resources/META-INF/MANIFEST.MF
and main class is com.rh.xxx.Application, but still getting
Could not find or load main class...
Set the Start-Class attribute value in MANIFEST.MF file with the fully qualified java class. Verify the same in the generated jar file after the jar is created.
Please refer below, here starter class is the one has main method.
Content of, META-INF/MANIFEST.MF
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.mycompany.project.MyApplication
Refer https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-launcher-manifest for more info.

Do not understand following use of -classpath by javac

I am not very clear with the following question from SCJP Book (I read the solution and explanation though) ..
Consider the following directory structure :-
foo --> test --> xcom --> A.class, B.java
Here foo, test and xcom are directories. A.class and B.java are the files in xcom directory.
Following are the source codes of corresponding files:-
A.java
package xcom;
public class A { }
B.java
package xcom;
public class B extends A { }
The default classpath is /foo.
Now, in order to compile B.java, I keep my current directory as test and give :-
javac -classpath xcom xcom/B.java
Here I give the classpath as xcom which has A.class. But still it does not find class A. Why is it so??
If your classes are in package xcom, then your classpath needs to be at the directory directly above that. In this case, the classpath should be foo/test.
And if your current directory is foo/test, then this should be your javac:
javac -classpath . xcom/B.java
Because you have to specify classpath root to -classpath argument, like javac -classpath . xcom/B.java. To compile class B java compiler requires class A, it tries to locate class A file in {classpathroot}/xcom/.
Note: . - is a current directory
I think the root cause here is a misunderstanding of a "fully-qualified name" in Java.
The fully-qualified names of your two classes are xcom.A and xcom.B. Their source is in files A.java and B.java in a directory named xcom; the fully-qualified names dictate the directory structure. When you are going to use the files, either to compile them or run them, the classpath contains one or more locations from which the fully-qualified names can be found; so java is looking for xcom\A.java and xcom\B.java (when compiling) and xcom\A.class and xcom\B.class (when running).
That is why the classpath needs to specify the directory that contains xcom.
As you progress to more complex environments: the classpath can be a list of such locations; each location is separated by a semicolon on windows and a colon on unix systems. Each location can be a directory, as you've already seen, but it can also be a jar file. jar files are in zip file format, and zip files have a directory structure just like disks do. So you could zip up your class files, maintaining their xcom parent (but not their full paths), and specify the jar file in the classpath instead of a directory.
I know the question was already answered somewhat, but thought you might like the background explanation as well.

Why classpath=/tomcat_lib/ doesn't work

HI guys,
There is an abc.jar under /tomcat_lib. I need use this in my def.java
I tired
javac -classpath /tomcat_lib/ -d
../classes def.java
but it doesn't work
But if it works if I use
javac -classpath /tomcat_lib/abc.jar
-d ....
Can anyone help explain it?
To add a jar to your classpath, you need to specify the path up to and including the .jar file.
Quoting the official Java SE 6 documentation at Oracle.com:
Each [item in your classpath] should
end with a filename or directory
depending on what you are setting the
class path to:
For a .jar or .zip file
that contains .class files, the class
path ends with the name of the .zip or
.jar file.
For .class files in an
unnamed package, the class path ends
with the directory that contains the
.class files.
For .class files in a
named package, the class path ends
with the directory that contains the
"root" package (the first package in
the full package name).
...and from the "Folders and Archive Files" section of the same documentation:
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case, C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file.

Main-Class can't be set to existing class

I have a build setup that requires that my classes be put into a folder in a subdirectory of my current directory. I.E., if I'm at '.', then the classes could be in './somewhere_else/'
The problem is that when I do this I can't set a value for Main-Class that can find the main class. I have no problem when I build the jar from files at '.', but all the following attempts for main class result in java.lang.NoClassDefFoundError's:
Main-Class: ClassName
Main-Class: FolderName.ClassName
Main-Class: FolderName/ClassName
What should I be using?
The classes inside your jar have to be in the same directory structure as your package structure.
So, if you want to have your class ClassName (without any package) to be the main class, it has to be in the root directory of your jar.
If you want to have the class in the directory FolderName, this same name has to be the package name, meaning the first (non-empty non-comment) line of your source file should be package FolderName;.
If your problem is only the build setup - this is not a problem, the folder-layout of your directories outside the jar does not necessarily have to be the same as the layout inside your jar. But it still helps if the folder-structure fits to the package-structure (with maybe additionally directories above).
I think you should reference Main-Class simply by the class name (without directory structure). What will help you solve this problem is what you put as the classpath. Make sure the directory of Main-Class is in the classpath.
If your ClassName resides in package FolderName, then, it should be:-
Manifest-Version: 1.0
Main-Class: FolderName.ClassName
Class-Path: <third-party>.jar <another-third-party>.jar
They are case-sensitive, by the way.

Categories

Resources