I've been trying to use Matlab's javabuilder package under Windows XP, but I'm getting a strange error when trying to instantiate any javabuilder class. To illustrate the problem, I've created a simple program that prints the MCRROOT and PATH system variables (to check if they're correctly set) and tries to create a MWCharArray:
import com.mathworks.toolbox.javabuilder.*;
import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration;
class Main
{
public static void main(String[] args)
{
System.out.println("MCRROOT: " + System.getenv("MCRROOT"));
System.out.println("PATH: " + System.getenv("PATH"));
System.out.println(MCRConfiguration.isInstalledMCR());
MWCharArray test = new MWCharArray("Test");
}
}
When I execute the program, the output is:
MCRROOT: C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
PATH: C:\Program files\CollabNet Subversion Client;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
false
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getMCRRoot(MCRConfiguration.java:77)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ModuleDir.<clinit>(MCRConfiguration.java:51)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getModuleDir(MCRConfiguration.java:56)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.<clinit>(MWMCR.java:1447)
at com.mathworks.toolbox.javabuilder.MWUtil.GetUnknownClassID(MWUtil.java:1258)
at com.mathworks.toolbox.javabuilder.MWClassID.<clinit>(MWClassID.java:41)
at com.mathworks.toolbox.javabuilder.MWCharArray.<init>(MWCharArray.java:75)
at Main.main(Main.java:11)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.get(MCRConfiguration.java:70)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.<clinit>(MCRConfiguration.java:72)
... 8 more
Java Result: 1
First of all, are MCRROOT's and PATH's values correct? I've tried google for finding out how to set MCRROOT, but there are conflicting results: some sources say that I should include de version dir, others say the opposite. Also, why is the isInstalledMCR method returning false? I've double-checked the MCR installation (and even uninstalled and installed it to be sure), so why isn't the library finding it?
Thanks on advance for any help!
Edit: I've also tried setting MCRROOT with no version string, and it also fails.
Just wild guessing! Java is messing around with strings, while your 'mcrroot' contains white spaces. I might change the mcr install path to something like C:\MATLAB\MATLABCompilerRuntime\v710, omitting any white spaces and special characters.
I've found the solution, so I'm post a self answer for future reference: Besides adding the javabuilder.jar to the program's classpath, you also have to add the path to the MCR's runtime libraries to the java.library.path JDK parameter.
My mistake was that, instead of setting the path as the path to the libraries at the MCR installation directory (On my case, C:\MATLAB\MCR\v710\runtime\win32), I copied the runtime directory to my project's dir and used it instead. It seems that the javabuilder library uses the java.library.path variable the guess the MCROOT, what would explain the weird "StringIndexOutOfBoundsException".
Related
So far I have downloaded Apache Commons library , extracted the library
commons-lang3-3.8.1.jar in Java\jdk1.8.0_172\jre\lib\ext.
Now I have created a class with two fields and I want to compare two objects using
ob1.equals(ob2). Method equals and hashCode have been overridden and the error I'm getting is Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/builder/EqualsBuilder at runtime.
import java.util.*;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
class key{
int end;
LinkedList<Integer> via = new LinkedList<>();
key(int x,LinkedList<Integer> ob){
this.end = x;
this.via = ob;
}
#Override
public int hashCode(){
return new HashCodeBuilder().append(end).append(via).toHashCode();
}
#Override
public boolean equals(Object obj)
{
if(!(obj instanceof key))
return false;
if(this==obj)
return true;
key o=(key)obj;
return new EqualsBuilder().append(end,o.end).append(via,o.via).isEquals();
}
}
class main{
public static void main(String[] args)
{
key ob1 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));
key ob2 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));
System.out.println(ob1.equals(ob2)); //expecting true
}
}
The details of the error are given below.
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/lang3/builder/EqualsBuilder
at key.equals(test.java:29)
at main.main(test.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.builder.EqualsBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
I have been facing this issue for a long time. I have checked all the class files and I'm quite sure that the libraries are loaded properly but I don't know why I'm getting NoClassDefFoundError at the runtime.
After spending hours on this issue I finally fixed it by setting the CLASSPATH variable.
I tried using -cp command but unfortunately that didn't work for me. If we do this explicitly, then you don't need to supply a "-cp" or "-classpath" switch value to the java compiler and java interpreter, since they will already know the updated classpath.
On my windows machine, I have set the CLASSPATH variable via the following:
set CLASSPATH=/coding #October\lib\commons-lang3-3.8.1.jar;.
Currently, I'm in coding #October directory. The commons-lang3-3.8.1.jar file is located in the coding #October\lib directory.The myapp.java file is located in the coding #October directory.
After setting the classpath, I can then compile and execute myapp.java via
javac myapp.java command directly and then java myapp to execute the script.
You placed the jar in the correct jre\lib\ext relative path... but it will work only if the java command you run comes from the jre\bin directory of the same jre path where you did the change.
If you copied the correct jar in the extension directory but you get this exception it very probably means that as you run your program you don't use the JRE where you did the change but another one.
The java command from the PATH env variable very probably doesn't refer to the JRE you extended. You can display PATH in your shell to check that.
So either set the PATH with the java home path of the JRE you extended or just run the java command by specifying the absolute path such as /foo/jre/bin/java main.
It should (to not say has to) work.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at io.appium.java_client.internal.ElementMap.getElementClass
Answer: add selenium jar "commons-lang3-3.8.1" for resolving this issue
I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs
We have a TeamCity (7.0.3) agent running on a 64-bit Windows Server 2008 machine. When we recently upgraded the agent to use Java 7 (1.7.0_10) the builds started failing with the following stacktrace:
Error occurred during initialization of VM
java.lang.ExceptionInInitializerError
at java.lang.Runtime.loadLibrary0(Runtime.java:841)
at java.lang.System.loadLibrary(System.java:1084)
at java.lang.System.initializeSystemClass(System.java:1145)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at java.io.Win32FileSystem.<init>(Win32FileSystem.java:40)
at java.io.WinNTFileSystem.<init>(WinNTFileSystem.java:37)
at java.io.FileSystem.getFileSystem(Native Method)
at java.io.File.<clinit>(File.java:156)
at java.lang.Runtime.loadLibrary0(Runtime.java:841)
at java.lang.System.loadLibrary(System.java:1084)
at java.lang.System.initializeSystemClass(System.java:1145)
The problem seems to be caused by the inclusion of the "-Dfile.separator=\" java option that TeamCity uses in the executable command for the agent. I was able to reproduce the problem by writing a simple "Hello World" class and compiling it on the Windows box and then running the program with the file.separator option (i.e. java -Dfile.separator=\ HelloWorld)
I haven't found any similar bug reports. Has anyone seen anything like this? Has the behaviour of file.separator changed in Java 7?
Furthermore I realise that \ is the default file.separator for Windows anyway so I don't think the agent really needs to use it in the executable command, however I can't see a way in TeamCity to tell the agent not to include it. Is it possible to do this?
Try the JVM command line parameter -Dfile.separator=\/ (i.e., specify both a backward and forward slash).
It looks java.exe now trims trailing \ (back-slash).
I have the following code:
import java.lang.*;
public class test {
public static void main(String[] argz) {
for(String s : argz) {
System.out.println("agg=" + s + "|");
}
System.out.println("prop=" + System.getProperty("prop") + "|");
}
}
I start it with Java 1.7.0_07 and _10:
C:\Java\jdk1.7.0_07\bin\java.exe -cp . -Dprop=z\\ test a\\ b
agg=a\\|
agg=b|
prop=z\\|
and _10
C:\Java\jdk1.7.0_10\bin\java.exe -cp . -Dprop=z\\ test a\\ b
agg=a|
agg=b|
prop=z|
And one more series:
C:\Java\jdk1.7.0_07\bin\java.exe -cp . -Dprop=z\ test a\ b
agg=a\|
agg=b|
prop=z\|
and _10
C:\Java\jdk1.7.0_10\bin\java.exe -cp . -Dprop=z\ test a\ b
agg=a|
agg=b|
prop=z|
Is there a way to convert JAR lib into JAR standalone?
I need to find a standalone java executable that convert PDF into TIFF and I've found these JARs: http://www.icefaces.org/JForum/posts/list/17504.page
Any ideas?
Easiest might be to create another Jar with a Main() entry point, and then just use the java.exe executable to run it:
e.g.
> java.exe -cp MyJarMain.jar;MyPDFJar.jar com.mydomain.MyMain myPDF.pdf
Where MyMain is a class with a Main static method.
You'll need something with a main entry point to pass in and interpret some command line arguments (myPDF.pdf in my made-up example)
You could do an assembly (are you using maven?) and make sure the Main-Class entry in the manifest.mf points to the main class.
Since there is no main-Method, you have to write one, or write a whole new class to call the class/method TiffConver.convertPDF .
The question is, how you're going to use it. From the command line, you need no executable jar. From the Gui, maybe you want to pass a file to be converted by drag and drop? Then you should take the parameter(s) passed to main as Input-PDF-Names (if they end in .pdf) and pass the names iteratively to TiffConverter, for "a.pdf b.pdf" =>
TiffConver.convertPDF ("a.pdf", "a.tiff");
TiffConver.convertPDF ("b.pdf", "b.tiff");
TiffCoverter will silently overwrite existing tiffs, so check that before or change the code there - this is clearly bad habit, and look out for more such things - I didn't.
/*
* Remove target file if exists
*/
File f = new File(tif);
if (f.exists()) {
f.delete();
}
Maybe you wan't to write a swing-wrapper, which let's you choose Files interactively to be converted. This would be a nice idee, if no filename is given.
If the user passes "a.pdf xy.tiff" you could rename the converted file to xy, as additional feature.
Without a main-class, however, a standalone jar would be magic.
However, building a native executale is almost always a bad idea. You loose portability, you don't profit from security- and performance improvements to the JVM or fixed bugs. For multiple programs you need always an independend bugfix, which you might have to manage yourself, if you don't have a package-management as most linux distros have.
after clearing some questions:
public static void main (String [] args) {
if (args.length == 1 && args[0].endsWith (".pdf")) {
String target = args[0].replaceAll (".pdf$", ".tif");
convertPDF (args[0], target);
}
}
This method you put into TiffConvert. It will allow you to convert a simple pdf-File, and generate a tif-File with the same basename but ending in .tif, silently overwriting an existing one of the same name.
I guess you now need to know how to start it?
I know. Heresy. But I'm in a bind. I have a lot of config files that use absolute path names, which creates an incompatibility between OS X and Windows. If I can get OS X (which I'm betting is the more flexible of the two) to recognize Q:/foo/bar/bim.properties as a valid absolute file name, it'll save me days of work spelunking through stack traces and config files.
In the end, I need this bit of Java test code to print "SUCCESS!" when it runs:
import java.io.*;
class DriveLetterTest {
static public void main(String... args) {
File f = new File("S:");
if (f.isDirectory()) {
System.out.println("SUCCESS!");
} else {
System.out.println("FAIL!");
}
}
}
Anyone know how this can be done?
UPDATE: Thanks for all the feedback, everyone. It's now obvious to me I really should have been clearer in my question.
Both the config files and the code that uses them belong to a third-party package I cannot change. (Well, I can change them, but that means incurring an ongoing maintenance load, which I want to avoid if at all possible.)
I'm in complete agreement with all of you who are appalled by this state of affairs. But the fact remains: I can't change the third-party code, and I really want to avoid forking the config files.
Short answer: No.
Long answer: For Java you should use System.getProperties(XXX).
Then you can load a Properties file or Configuration based on what you find in os.name.
Alternate Solution just strip off the S: when you read the existing configuration files on non-Windows machines and replace them with the appropriate things.
Opinion: Personally I would bite the bullet and deal with the technical debt now, fix all the configuration files at build time when the deployment for OSX is built and be done with it.
public class WhichOS
{
public static void main(final String[] args)
{
System.out.format("System.getProperty(\"os.name\") = %s\n", System.getProperty("os.name"));
System.out.format("System.getProperty(\"os.arch\") = %s\n", System.getProperty("os.arch"));
System.out.format("System.getProperty(\"os.version\") = %s\n", System.getProperty("os.version"));
}
}
the output on my iMac is:
System.getProperty("os.name") = Mac OS X
System.getProperty("os.arch") = x86_64
System.getProperty("os.version") = 10.6.4
Honestly, don't hard-code absolute paths in a program, even for a single-platform app. Do the correct thing.
The following is my wrong solution, saved to remind myself not to repeat giving a misdirected advice ... shame on me.
Just create a symbolic link named Q: just at the root directory / to / itself.
$ cd /
$ ln -s / Q:
$ ln -s / S:
You might need to use sudo. Then, at the start of your program, just chdir to /.
If you don't want Q: and S: to show up in the Finder, perform
$ /Developer/Tools/SetFile -P -a V Q:
$ /Developer/Tools/SetFile -P -a V S:
which set the invisible-to-the-Finder bit of the files.
The only way you can replace java.io.File is to replace that class in rt.jar.
I don't recommend that, but the best way to do this is to grab a bsd-port of the OpenJDK code, make necessary changes, build it and redistribute the binary with your project. Write a shell script to use your own java binary and not the built-in one.
PS. Just change your config files! Practice your regex skills and save yourself a lot of time.
If you are not willing to change your config file per OS, what are they for in first place?
Every installation should have its own set of config files and use it accordingly.
But if you insist.. you just have to detect the OS version and if is not Windows, ignore the letter:
Something along the lines:
boolean isWindows = System.getProperty("os.name").toLowerCase()
.contains("windows");
String folder = "S:";
if (isWindows && folder.matches("\\w:")) {
folder = "/";
} else if (isWindows && folder.matches("\\w:.+")) {
folder = folder.substring(2);// ignoring the first two letters S:
}
You get the idea
Most likely you'd have to provide a different java.io.File implementation that can parse out the file paths correctly, maybe there's one someone already made.
The real solution is to put this kind of stuff (hard-coded file paths) in configuration files and not in the source code.
Just tested something out, and discovered something interesting: In Windows, if the current directory is on the same logical volume (i.e. root is the same drive letter), you can leave off the drive letter when using a path. So you could just trim off all those drive letters and colons and you should be fine as long as you aren't using paths to items on different disks.
Here's what I finally ended up doing:
I downloaded the source code for the java.io package, and tweaked the code for java.io.File to look for path names that start with a letter and a colon. If it finds one, it prepends "/Volumes/" to the path name, coughs a warning into System.err, then continues as normal.
I've added symlinks under /Volumes to the "drives" I need mapped, so I have:
/Volumes/S:
/Volumes/Q:
I put it into its own jar, and put that jar at the front of the classpath for this project only. This way, the hack affects only me, and only this project.
Net result: java.io.File sees a path like "S:/bling.properties", and then checks the OS. If the OS is OS X, it prepends "/Volumes/", and looks for a file in /Volumes/S:/bling.properties, which is fine, because it can just follow the symlink.
Yeah, it's ugly as hell. But it gets the job done for today.