I've a java program as shown below:
package com.abc.myproject;
import java.util.*;
class test {
public static void main(String[] args)
{
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("name");
System.out.println("propertyValue="+propertyValue);
}
}
I've this program located under C:\testing\code\src\com\abc\myproject\test.java
And, I've kept mybundle.properties under C:\testing\code folder.
mybundle.properties contain one line:
name=Mike
When I run the program from command prompt as shown below, it runs perfectly fine:
C:\testing\code\src>java -cp .;c:\testing\code com.abc.myproject.test
propertyValue=Mike
Now, I created Jar file xyz.jar containing only test.class & kept this Jar in C:\testing directory & the manifest of jar contains below:
Manifest-Version: 1.0
Created-By: 1.7.0_05 (Oracle Corporation)
Main-Class: com.abc.myproject.test
In the same directory C:\testing, I kept mybundle.properties & then tried to run jar with below commands:
java -cp .;c:\testing -jar xyz.jar
But it fails to load mybundle resource & throws error:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle
for base name scheduler, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.abc.myproject.test.main(test.java:8)
I have to keep the mybundle.properties out side of Jar since we should be able to make changes in this file without having to redeploy the Jar.
Can any one please help me in fixing this issue?
Thanks!
java -cp .;c:\testing -jar xyz.jar
You cannot add to the classpath when using -jar. $CLASSPATH and -cp are ignored, and it will only use the classpath in the jar manifest.
What you can do is run your class like this:
java -cp .;c:\testing;xyz.jar com.abc.myproject.test
Related
I am trying to get a simple JAR file executing on the CLI (without a manifest). The folder structure is as follows:
mods\un.jar
out\test\Main.class
src\test\Main.java
Here is Main.java:
package test;
public class Main{
public static void main(String []args){
System.out.println("Unnamed module...");
}
}
Here are the commands from the root folder:
javac -d out src\Main.java
java -cp out test.Main - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR
The last line generates the errors:
Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main
I have looked at the JAR zip and it looks ok. The MANIFEST.MF is the default one but that is okay as I am explicitly telling Java what class to start with (the class with main()). The contents are:
META-INF\MANIFEST.MF
out\test\Main.class
The manifest itself is:
Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)
So it looks like the class file in the JAR is not being picked up at all. If I omit the out folder when I run java or if I rename the .class file in the out folder), I get the class not found error. I would like to get it working without a manifest.
Any ideas?
Thanks,
Seán.
You need to change to the 'out' directory to get your package root correct in the jar:
jar -cvf mods\un.jar -C out test\Main.class
I am new to JAVA9 modular architecture and trying to compile and run module inside a JAR from windows command line.
I have created simple HelloWorld.java main class and project architecture is as below :
I was successfully able to compile and create JAR file using following command.
Compile :
javac -d target/HelloWorld src/HelloWorld/com/java/modularity/test1/HelloWorld.java src/HelloWorld/module-info.java
Create a HelloWorld.jar file in "jarfile" directory :
jar -cfe jarfile/HelloWorld.jar com.java.modularity.test1.HelloWorld target/HelloWorld/module-info.class target/HelloWorld/com/java/modularity/test1/HelloWorld.class
Getting following error while trying to run module from JAR file :
D:\sts_workspace\java9tutorial>java -p jarfile -m HelloWorld
module HelloWorld does not have a ModuleMainClass attribute, use -m <module>/<main-class>
Getting following error while slide change in command :
D:\sts_workspace\java9tutorial>java -p jarfile -m target/HelloWorld/com.java.modularity.test1.HelloWorld
Error occurred during initialization of boot layer
java.lang.module.FindException: Module target not found
Here is my entry class HelloWorld.java :
package com.java.modularity.test1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Testing first HelloWorld module ...");
}
}
Here is HelloWorld module description :
module HelloWorld {
}
I also tried by extracting my generated HelloWorld.jar file and "Main-Class" attribute is also present in MANIFEST.MF file :
Manifest-Version: 1.0
Created-By: 1.8.0_172 (Oracle Corporation)
Main-Class: com.java.modularity.test1.HelloWorld
Do I need to export the entry class in my module definition ? Any suggestion will help me to fix the issue.
Try changing
-m target/HelloWorld/com.java.modularity.test1.HelloWorld
to
-m HelloWorld/com.java.modularity.test1.HelloWorld
for the syntax is module[/mainclass]
I apoligize if this has been asked before but I have been unable to find anything that is the same.
I created a simple jar(myFailingJar.jar) file with two classes a main class and a simple class that accesses a class and its functions from another jar file (CCJAPI.jar).
The main class just instantiates the simple class, the simple class loads a shared library object and calls a function within CCJAPI.jar that crosses over JNI.
When run as a jar file with this command it fails because it can't find a class in CCJAPI.jar which is on the classpath:
java -classpath /home/scott:/home/scott/CCJAPI.jar -jar myFailingJar.jar
Starting
Exception in thread "main" java.lang.NoClassDefFoundError: ccjni/DeviceManager
at DetachedManager.DetachedDeviceManager.startManager(DetachedDeviceManager.java:24)
at DetachedManager.Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: ccjni.DeviceManager
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
If I extract the contents of the myFailingJar.jar file and run with this command it works, which as far as I can tell just goes to the extracted class files and runs the :
java -classpath /home/scott:/home/scott/CCJAPI.jar DetachedManager.Main
Starting
** Started **
Success = - Going to crash now
Here is both source files contents:
Source of Main
package DetachedManager;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
DetachedDeviceManager devMgr = new DetachedDeviceManager();
if( devMgr.startManager() )
{
System.out.println("Success = - Going to crash now");
}
}
}
Source of simple class:
package DetachedManager;
import ccjni.DeviceManager;
public class DetachedDeviceManager {
{
System.load("/usr/lib/libccJNI.so");
}
public boolean startManager()
{
System.out.println("Starting");
DeviceManager.start();
System.out.println("** Started ** ");
return true;
}
}
The only difference is that one is tyring to run the compiled class from within the jar file and the other is running it outside the jar file. It must be some type of classpath or path issue that I have not been able to figure out. Any help would be much appreciated.
You probably are not creating the correct jar with the correct dependancies. Use the eclipse export (Right click on a project --> Export), This will also create the required manifest file.
Using this should be helpful.
When I specified the classpath on the command line I thought that should be sufficient for any jar file loaded into the java environment to find a jar file, apparently it is not. I ended up having to add the class path into the manifest file even though that path was specified on the command line.
Here is the failing manifest:
Manifest-Version: 1.0
Created-By: 1.6.0_0 (Sun Microsystems Inc.)
Main-Class: DetachedManager.Main
Here is the successful manifest
Manifest-Version: 1.0
Created-By: 1.6.0_0 (Sun Microsystems Inc.)
Class-Path: lib/CCAPI.jar
Main-Class: DetachedManager.Main
Thanks to all for listening and giving me an idea of where and what to look at.
I created the following class located in the MainJPrint.java file
import com.XXXXX.pdfPrint.PDFPrint;
public class MainJPrint
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
print(".....");
}
public static String print (final String url)
{
Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
...
}
}
}
}
In the same folder I have a jar archive jPrint.jar
I compile the class using the following command
>javac -classpath jPrint.jar MainJPrint.java
When I'm trying to execute resulted class file, I get this error:
>java MainJPrint
java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint
If I uncomment the Hello World line and comment the next line, the program runs fine.
I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2.
I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)
The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like
>C:\j2sdk1.4.2\bin\java.exe MainJPrint
jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.
I tried other options like:
>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar
So how can I execute that class file which uses a class located in a separate archive?
To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.
If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.
But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:
java -cp jPrint.jar:. MainJPrint
Or on Windows:
java -cp jPrint.jar;. MainJPrint
I am trying to run simple java code on VMWare Workstation. I have the following simple test Main file:
import cern.jet.random.engine.RandomSeedGenerator;;
public class TestDataService {
//private static Logger logger = Logger.getLogger(TestDataService.class);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World DAI!");
// Input some data.
RandomSeedGenerator re = new RandomSeedGenerator();
return;
}
}
RandomSeedGenerator is a class in colt.jar library, and I have the jar file under my lib folder.
I am building the project with ant, and I have the following manifest file where I set the classpath:
Manifest-Version: 1.0
Main-Class: edu.umass.TestDataService
Name: edu/umass/TestDataService/Version.class
Class-Path: lib/colt.jar
When I run the code from the VMWare shell which runs Red Hat Linux, I get this Exception:
[root#localhost] java -jar app.jar
Hello World DAI!
Exception in thread "main" java.lang.NoClassDefFoundError: cern/jet/random/engine/RandomSeedGenerator
at edu.umass.TestDataService.main (Unknown Source)
Caused by: java.long.ClassNotFoundException: cern.jet.random.engine.RandomSeedGenerator
Just as a final note, everything seems to work fine on windows with eclipse, but nothing seems to work on the virtual machine. Any ideas?
Did you install the jar files required by your application on the VMs?
Did you configured CLASS_PATH correctly?
I doubt there is an issue with the jvm or the vm. The problem is going to be in how you run the class. Specifically how your setting the classpath. Try this:
Navigate to where you've placed colt.jar. Get the present working directory by typing in pwd. Use this to construct the run command using the absolute path to colt.jar.
So eventually you should be running (from the directory containing your jar) something like this:
java -cp /the/full/path/to/lib/colt.jar -jar app.jar
Once you've got that work you can then try and figure out what the correct relative path is. and then you'll be able to do
java -cp a/relativel/path/to/lib/colt.jar -jar app.jar