1.Create a simple java project with below structure and code
Javalearning/src/com/learn/Main
Main is the java file where it has the below code :
package com.learn;
public class Main {
public static void main(String[] args) {
System.out.println("Java learning");
}
}
2.Ran the java code through InteliJ and it is working as expected with below response
Java learning
Process finished with exit code 0
3.Create a class file through command line by below command
javac Main.java
4.Create a Manifest.txt file with below content
Manifest-Version: 1.0
Created-By: rock
Main-Class: Main.class
5.Create jar file by using below command
jar cfm MyJar.jar Manifest.txt Main.class
6.Now try to execute the JAR using below command
java -jar myJar.jar
7.Above command throwing below error message in terminal
Caused by: java.lang.ClassNotFoundException: Main.class
8.When I open the jar from the below tool , I can find META-INF folder and Main.class file. Inside META-INF folder I can found MANIFEST.MF
Can someone suggest why the jar execution throwing ClassNotFoundException and how can we resolve it ?
Related
Need some clever advice to solve this - well, some kind of problem while using simple JavaFX app.
However, there was some problems while all is set-up (module path, for example), but what now when I just cannot run .jar file from terminal? Ok, start talk about mission-impossible-with-javafx:
I got Apache NetBeans 12 installed on Linux Mint 20. JDK is already installed, so 'java -version' returns 'openjdk version "11.0.7" 2020-04-14'...
When start project there is no problem at all - JavaFX app works.
I need this app to work on Windows 8.1, but before that I tried to run this .jar file on my linux terminal using java -jar MyFile.jar.
Here the problem starts: it said no main manifest attribute, in MyFile.jar.
Then I decide to set the main class directly in jar file (opened using Archive Manager):
MyFile.jar -> META-INF -> MANIFEST.MF and add on last line: Main-Class: mypackage.MyClass
Now I have: Error: Could not find or load main class mypackage.MyClass Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Ok, how and why?
Just to said that in NetBeans I also set main class in:
Project -> Properties -> Run -> Main class: mypackage.MyClass
and in VM options I added:
--module-path "/locationToJavaFX/javafx-sdk-11.0.2/lib" --add-modules javafx.controls,javafx.fxml
In pom file, main class is also set (generated by IDE):
// other lines
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>mypackage.MyClass</mainClass>
</configuration>
// other lines
and module-info looks like:
module mypackage {
requires javafx.controls;
requires javafx.fxml;
requires org.jsoup;
opens mypackage to javafx.fxml;
exports mypackage; // some people advice without this line, I tried but same problem
}
Just want to be clear - I tried probably everything (I don't count) that is available here on stackoverflow. I try to fix this yesterday and today (almost 2 whole days) and I can't.
Just a thought: Java programming is not so difficult as configuration is.
Cheers and thanks for helping!
EDIT:
Just to said that I tried what José suggested : first creating launcher class, follow 'fat jar' step-by-step then run 'semi-fat jar' command which he added to the post. Interestingly enough, this return me: Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx-fxml not found. Why not found javafx-fxml if I aready defined it as said above in question? Also, why Netbeans doesn't provide me the main class in manifest (in jar) file? When I add main class in manifest, I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
Ok, how Application does not found? :)
EDIT 2:
Yeap, mister José has a right. This command in his 'semi-fat jar' post actually works BUT not for Maven (I use it on same Ant project and it works). Maven somehow cannot recognize JSoup class (even I add it in dependency) and java returns:
Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at p.eis.GetData1.startIt(GetData1.java:34)
at p.eis.GetData1.<init>(GetData1.java:28)
at p.eis.SampleController.initialize(SampleController.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at p.eis.Start.loadFXML(Start.java:32)
at p.eis.Start.start(Start.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 17 more
Hope I'll find solution for Maven projects in the near future.
Yeah I noticed you use maven but it's not fault of maven, I know why this happen and i have solution about it so in my solution there is magic and work fine because i use my solution to make jar and you know my jar file run without terminal(means double clicking on jar file)
Solution
you need winRar because i use it for info read my solution!
Just follow below step with images to run jar(both maven and gradle project)
solution of Error: Could not find or load main class mypackage. this line
you created main class at this location /src/main/java/YOUR_PACKAGE_NAME/Main.java so while creating jar file intellij couldn't care your main class if you create main class at above location and it doesn't add Main-Class: Main this line in Manifest File Solution is that create one more main class(don't delete Main.Java class we use this class in new one main class) at this location /src/main/java/EntryPoint.java see below image and code what i did
EntryPoint.java is new main class (IMAGE)
public class EntryPoint {
public static void main(String[] args) {
smartSeizeing.Main.main(args);
}
}
Main.java Image
Main.Java (IMAGE)
After creating EntryPoint.java(New Main Class) Manifest File For Jar File, clicking Cntr+Alt+Shift+S to create Manifest File | Jar File, during creating jar file(before build jar) Just select EntryPoint.Java As Main Class Below Image
Main Class : EntryPoint (IMAGE)
Before click Ok button change the location of manifest file create it at this location /src/META_INF/manifest.mf(Wrong location : /src/main/META_INF/menifest.mf) and click Apply button to save changes
Location Of Manifest File like below image
Just Manifest.MF File (IMAGE)
Build Jar file go to Option[Build->Build-Artifacts...] and take 3-5 min to create jar.
After Creating Jar file Please Read clearly this main step means
Means Solution Of This line of NOT FOUND FXML FILE this is tricky because all fxml file are stored at resource directory so while creating jar so intellij doesn't add the any file from resource directory again not include fxml file while creating jar
Solution Of How to include FXML FILE NOW WE HAVE TO USE WIN_RAR Software To Add This File And Believe Me Its work and not affect any file and work file Just Follow below step to add FXML FILE
Adding FXML FILE STEP
close Intellij
Go to your project Directory in my case C:\Users\ADMIN\IdeaProjects\YOUR_PROJECT_NAME and Go to YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME\YOUR_PROJECT_NAME.Jar so u find jar file of your project
Just click right key of Mouse for option, select option of EXTRACT HERE(WinRar Option)
JUST I AM ExtractING MY JAR (IMAGE)
DELETE ONLY JAR FILE AFTER EXTRACTING Like my file name smartSeizeing.jar i delete it (IMAGE)
After extracting and deleting jar(only jar file) file so OPEN YOUR_PACKAGE_NAME File in My case C:\Users\ADMIN\IdeaProjects\SmartSeizeing\out\artifacts\SmartSeizeing_jar\smartSeizeing
Keep Open It And Open Your Project directory and copy all file(including all file like fxml, css, html, hibernet file if u have other file to with fxml) from resource directory and Paste all file(copied Files) in that previously open folder(extract jar folder like IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar\YOUR_PACKAGE_NAME) And after coping file so and just make jar file select all file(IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar) and click right key of mouse, select add to archive(WinRar) and after creating YOUR_PACKAGE_NAME.jar file just double click that file and see the magic
How to Add fxml and how to create again jar(without any curreption) i mentioned in below image so u can easily understand what i did and also image of my OUTPUT
i pasted all file of resource directory of my project (IMAGE)
Please see below answer too because i can't add more link for step of how to make jar using winRar and that jar work fine and also my output
Best Of Luck
and You wanna video just tell me i make video about it How to make runnable jar without any exception or error!
this is because i can't add more links that's why i again wrote this
WinRar to create Jar File
Continue From Previous Answer and image that is https://i.stack.imgur.com/fY1tw.png
this is what i copy from Resource folder of my project
just paste of all file of resource directory at (in my case) C:\Users\ADMIN\IdeaProjects\SmartSeizeing\out\artifacts\SmartSeizeing_jar\smartSeizeing
WinRar to make Jar follow bellow step (IMAGE)
Step 1
Step 2 select zip option then change extension of your file .zip to .jar
Step 3
This is what i am trying to explain to you and this is my Output
i hope you understand what i do for creating jar file
I'm trying to call Haskell code from Java, using JavaCPP to help create the necessary JNI binding, as already discussed in this question.
This is how I'm using it:
<rootdir>
/javacpp.jar
/build (destination of libraris)
/src (contains Haskell code)
/com/example/HSCode.java (Java class to load and use native lib)
Content of HScode.java:
package com.example;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
#Platform(include={"<HsFFI.h>","HScode_stub.h"})
public class HScode {
static { Loader.load(); }
public static native void hs_init(int[] argc, #Cast("char***") #ByPtrPtr PointerPointer argv);
public static native String code_hs(String text);
public static void main(String[] args) throws FileNotFoundException {
String s = new Scanner(new File("test.txt")).useDelimiter("\\Z").next();
hs_init(null, null);
String s1 = code_hs(s);
System.out.println(s1);
}
}
Compilation:
cd <rootdir>
ghc --make -isrc -dynamic -shared -fPIC src/HScode.hs \
-o build/libHScode.so -lHSrts-ghc7.8.4 -optl-Wl,-rpath,.
javac -cp javacpp.jar com/example/HScode.java
java -jar javacpp.jar -d build \
-Dplatform.compiler=ghc -Dplatform.includepath="src:com/example" \
-Dplatform.compiler.output="-optl-Wl,-rpath,. -optc-O3 -Wall build/libHScode.so -dynamic -fPIC -shared -lstdc++ -lHSrts-ghc7.8.4 -o " com.example.HScode
Following this approach, I can create a libHScode.so and a libjniHScode.so using javacpp, which runs fine with:
$ java -cp javacpp.jar:. com.example.HScode
Jar
Now, the following step is that I want to package everything in a jar, and be able to use this jar's com.example.HScode from a larger java project.
JavaCPP's page mentions:
[...] Moreover, at runtime, the Loader.load() method automatically
loads the native libraries from Java resources, which were placed in
the right directory by the building process. They can even be archived
in a JAR file, it changes nothing. Users simply do not need to figure
out how to make the system load the files.
So I thought this should work.
However, if I make a jar HScode.jar out of the content of the build folder above, so that my jar contains both libjniHScode.so and libHScode.so, and run it with:
$ java -cp javacpp.jar:HScode.jar:. com.example.HScode
then it cannot find my native code (exception edited for anonymization):
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniHScode in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:597)
at org.bytedeco.javacpp.Loader.load(Loader.java:438)
at org.bytedeco.javacpp.Loader.load(Loader.java:381)
at com.example.HScode.<clinit>(HScode.java:13)
Caused by: java.lang.UnsatisfiedLinkError: /compilation-path/linux-x86_64/libjniHScode.so: HScode.so: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1937)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1822)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:580)
What am I missing? Does anyone know whether JavaCPP can actually find the native code when it's archived in a jar?
Building for the native libraries by calling javacpp -jar javacpp.jar com.example.HScode outputs them in com/example/linux-x86_64/ automatically and the Loader loads them from there. So when building the native libraries by some other means, they still need to be moved to com/example/linux-x86_64/, whether inside a JAR file or outside as normal files, if we want the Loader to find them.
I wrote a program that runs fine, however packaging the file into a jar file and trying to execute that fails.
The project consists of three files :
CinReader.java --> CinReader.class
PageSource.java --> PageSource.class
LocalBot.java --> LocalBot.class (main class)
The contents of manifest.txt are :
Main-Class: LocalBot.class
I created the jar file with the command :
C:\Users\ioil\Desktop\CSCI20\localBot>jar cvfm LocalBot.jar manifest.txt *.class
added manifest
adding: CinReader.class(in = 5908) (out= 3027)(deflated 48%)
adding: LocalBot.class(in = 362) (out= 272)(deflated 24%)
adding: PageSource.class(in = 5293) (out= 2560)(deflated 51%)
C:\Users\ioil\Desktop\CSCI20\localBot>
Now when I enter the command : "java -jar LocalBot.jar" or "LocalBot.jar" the Java Virtual Machine Launcher returns the error message : "Could not find the main class: LocalBot. Program will exit"
Am I doing anything obviously wrong . . .
Take a look at Understanding the Manifest
When you create a JAR file, it automatically receives a default
manifest file. There can be only one
manifest file in an archive, and it
always has the pathname
META-INF/MANIFEST.MF
I am currently working on a J2ME polish application, just enhancing it. I am finding difficulties to get the exact version of the jar file.
Is there any way to find the version of the jar file for the imports done in the class? I mean if you have some thing, import x.y.z; can we know the version of the jar x.y package belongs to?
Decompress the JAR file and look for the manifest file (META-INF\MANIFEST.MF). The manifest file of JAR file might contain a version number (but not always a version is specified).
You need to unzip it and check its META-INF/MANIFEST.MF file, e.g.
unzip -p file.jar | head
or more specific:
unzip -p file.jar META-INF/MANIFEST.MF
Just to expand on the answers above, inside the META-INF/MANIFEST.MF file in the JAR, you will likely see a line: Manifest-Version: 1.0 ← This is NOT the jar versions number!
You need to look for Implementation-Version which, if present, is a free-text string so entirely up to the JAR's author as to what you'll find in there.
See also Oracle docs and Package Version specificaion
Just to complete the above answer.
Manifest file is located inside jar at META-INF\MANIFEST.MF path.
You can examine jar's contents in any archiver that supports zip.
Each jar version has a unique checksum. You can calculate the checksum for you jar (that had no version info) and compare it with the different versions of the jar. We can also search a jar using checksum.
Refer this Question to calculate checksum:
What is the best way to calculate a checksum for a file that is on my machine?
Basically you should use the java.lang.Package class which use the classloader to give you informations about your classes.
example:
String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();
I think logback is known to use this feature to trace the JAR name/version of each class in its produced stacktraces.
see also http://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779
Thought I would give a more recent answer as this question still comes up pretty high on searches.
Checking CLi JAR Version:
Run the following on the CLi jar file:
unzip -p jenkins-cli.jar META-INF/MANIFEST.MF
Example Output:
Manifest-Version: 1.0
Built-By: kohsuke
Jenkins-CLI-Version: 2.210 <--- Jenkins CLI Version
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_144
Main-Class: hudson.cli.CLI
The CLi version is listed above.
To get the Server Version, run the following:
java -jar ./jenkins-cli.jar -s https://<Server_URL> -auth <email>#<domain>.com:<API Token> version
(the above will vary based on your implementation of authentication, please change accordingly)
Example Output:
Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider
INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider
2.210 <-- Jenkins Server Version
This simple program will list all the cases for version of jar namely
Version found in Manifest file
No version found in Manifest and even from jar name
Manifest file not found
Map<String, String> jarsWithVersionFound = new LinkedHashMap<String, String>();
List<String> jarsWithNoManifest = new LinkedList<String>();
List<String> jarsWithNoVersionFound = new LinkedList<String>();
//loop through the files in lib folder
//pick a jar one by one and getVersion()
//print in console..save to file(?)..maybe later
File[] files = new File("path_to_jar_folder").listFiles();
for(File file : files)
{
String fileName = file.getName();
try
{
String jarVersion = new Jar(file).getVersion();
if(jarVersion == null)
jarsWithNoVersionFound.add(fileName);
else
jarsWithVersionFound.put(fileName, jarVersion);
}
catch(Exception ex)
{
jarsWithNoManifest.add(fileName);
}
}
System.out.println("******* JARs with versions found *******");
for(Entry<String, String> jarName : jarsWithVersionFound.entrySet())
System.out.println(jarName.getKey() + " : " + jarName.getValue());
System.out.println("\n \n ******* JARs with no versions found *******");
for(String jarName : jarsWithNoVersionFound)
System.out.println(jarName);
System.out.println("\n \n ******* JARs with no manifest found *******");
for(String jarName : jarsWithNoManifest)
System.out.println(jarName);
It uses the javaxt-core jar which can be downloaded from http://www.javaxt.com/downloads/
I'm late this but you can try the following two methods
using these needed classes
import java.util.jar.Attributes;
import java.util.jar.Manifest;
These methods let me access the jar attributes. I like being backwards compatible and use the latest. So I used this
public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
String className = sourceClass.getSimpleName() + ".class";
String classPath = sourceClass.getResource(className).toString();
if (!classPath.startsWith("jar")) {
// Class not from JAR
return null;
}
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
"/META-INF/MANIFEST.MF";
Manifest manifest = new Manifest(new URL(manifestPath).openStream());
return manifest.getEntries().get("Build-Info");
}
public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);
String attribute = version_attr.getValue(attributeName);
return attribute;
}
This works well when you are using maven and need pom details for known classes. Hope this helps.
For Linux, try following:
find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' \;|awk -F ': ' '{print $2}'
If you have winrar, open the jar with winrar, double-click to open folder META-INF. Extract MANIFEST.MF and CHANGES files to any location (say desktop).
Open the extracted files in a text editor: You will see Implementation-Version or release version.
You can filter version from the MANIFEST file using
unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'
best solution that does not involve extracting the jar files is to run the following command. If the jar file does not contain a manifest file you will get a "WARNING: Manifest file not found"
java -jar file.jar -v
Just rename the extension with .zip instead of .jar. Then go to META-INF/MANIFEST.MF and open the MANIFEST.MF file with notepad. You can find the implementation version there.
It can be checked with a command java -jar jarname
when trying to execute my jar file I get an exception:
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory
[Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
I guess this is some kind of missing library on the classpath.
Can anyone tell me which jar-file is missing? I can't find the class weblogic.jndi.WLInitialContextFactory anywhere...
Thanks!
P.S.: I already have weblogic 10.0 jar included.
Check your server/lib/ folder to find wliclient.jar.
With Weblogic 12.1.3, you can find it here:
${INSTALL_DIR}/inventory/wlserver/server/lib/wlclient.jar
Step 1:
Go to E:\weblogic81\user_projects\domains\mydomain. Then type Setenv command. As follows
E:\weblogic81\user_projects\domains\mydomain>setenv
Step 2:
Weblogic.jar file is needed by your client application. It may contain in the following path E:\weblogic81\weblogic81\server\lib\weblogic.jar. so set the classpath for the this folder or copy this weblogic.jar file into your application-folder so that weblogic.jar file is available to your application first.
E:\weblogic81\user_projects\domains\mydomain>set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Step 3:
Go to domain folder in command prompt as shown above and set classpath.
To not to disturb other classpaths set classpath as:
set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Here (.) dot represents set classpath to current directory.
Step 4:
After classpath set run command STARTWEBLOGIC as follows:
E:\weblogic81\user_projects\domains\mydomain>STARTWEBLOGIC
Step 5:
Do not login to weblogic server. If you are already login just log out and write the following code in myeclipse or some other IDE.
Step 6:
package directory.service;
import java.util.*;
import weblogic.jndi.*;
import java.io.FileInputStream;
import javax.naming.*;
public class GetInitContext {
/**
* #param args
*/
public static void main(String[] args) {
try{
weblogic.jndi.Environment env=new weblogic.jndi.Environment();
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory(
weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
env.setProviderUrl("t3://localhost:7001");
env.setSecurityPrincipal("agni");
env.setSecurityCredentials("agnidevam");
Context context=env.getInitialContext();
System.out.println("got the initial context for weblogic server---> "+context);
context.createSubcontext("sone");
context.bind("agni one",new Integer(10));
context.createSubcontext("sone/sctwo");
context.bind("agni two",new Integer(20));
context.createSubcontext("sone/sctwo/scthree");
context.bind("agni three",new Integer(30));
System.out.println("subcontex object created please check in admin server for more details");
}
catch(Exception e){
System.out.println("file inputstream exception ---> "+e);
}
}
}
Step 7:
Execute the above code and login to weblogic and right click on myserver>view jndi tree> you find the bound objects information.
it looks you are doing a JNDI lookup outside of WLS.
You need to use wlfulclient.jar or if your machine has a WLS installation then add to your classpath project: WL_HOME/server/lib/weblogic.jar
I faced the same issue and it's fixed now :)
The fix is, to go to WebLogic server and navigate to /Oracle/Middleware/wlserver_10.3/server/lib/ and execute the below command.
Command: java -jar wljarbuilder.jar -profile wlfullclient5
The above command creates a jar file with all the jar's inside WebLogic server /lib folder and place it in your client java code build path Eclipse and craetes runnable JAR file and place this wlfullclient5.jar file in server/lib folder as well.
Hope this helps! Kindly let me know if you have any issues.
Adding wlserver/server/lib/weblogic.jar is enough. I test it.
Check the following tag in your build.xml
property name="WLS_HOME" value="${env.WLS_HOME}"
where WLS_HOME=c:\weblogic\wls\wlserver if running on windows
i kept trying to run a simple hello world program and it kept throwing
*run:
[echo] Executing client class
[java] javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]*
once i changed the above mentioned tag it in the build.xml it worked fine
It is packaged inside of the weblogic.jar under your server/lib.
in version 12c it is located in weblogic-classes.jar in your lib directory:
C:\wls1213\wlserver\server\lib
For WLS 12.2, where WL_HOME is The BEA home directory of your WebLogic installation
(as defualt WL_HOME is Middleware\Oracle_Home\wlserver)
%WL_HOME%\server\lib\wlclient.jar
%WL_HOME%\server\lib\wls-api.jar
%WL_HOME%\server\lib\wls-api-part.jar
%WL_HOME%\server\lib\wlthint3client.jar
all these libs contains the: jar: weblogic\jndi\WLInitialContextFactory.class
see WLS doc.: https://docs.oracle.com/en/middleware/fusion-middleware/weblogic-server/12.2.1.4/wlprg/overview.html#GUID-FC14CC53-DE49-456F-B54C-D73CC6DBF818
I've faced the issue stated here and I've managed to solved by fixing WL_HOME enviroment variable.
In my case the wlserver_10.3 folder was moved to another drive (From D to E) and the guy who did the disk "migration" forgot to change the WL_HOME value at PATH\TO\Oracle\Middleware\wlserver_10.3\common\bin
By fixing the wlserver_10.3 path I was able to deploy JAR's at WebLogic