I know that there is a plugin for ImageJ that handles NIfTI-1 files (http://rsb.info.nih.gov/ij/plugins/nifti.html).
But all its instructions on that page is to use ImageJ as a standalone program, however I am using its API. How can I know what methods are available in this jar file without its source?
I couldn't find the source code either.
For the supported archives in imageJ (such as DICOM) is quite easy :
public class ImageJTest {
public static void main(){
String path = "res/vaca.dcm";
FileInputStream fis;
ImageView image;
try {
fis = new FileInputStream(path);
DICOM d = new DICOM(fis);
d.run(path);
// Stretches the histogram because the pictures were being
// displayed too dark.
(new ij.plugin.ContrastEnhancer()).stretchHistogram(d, 0.1);
Image picture = SwingFXUtils.toFXImage(d.getBufferedImage(),
null);
// Makes the size standard for thumbnails
image = new ImageView(picture);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
How can I load the NIfTI-1 files in imageJ ?
Once you have the class files, which are embedded in the jar file (as #Alvin Thompson pointed out, these are just zip files by a different name), you can use the reflection API to mine the class files to get their methods. A sample follows for one class, cribbed from here:
Method[] methods = thisClass.getClass().getMethods(); // thisClass is an instance of the class you're working with
for(Method method : methods){
System.out.println("method = " + method.getName());
}
JAR files are just fancy ZIP files. You can rename the file to foo.zip, then use any unzip utility to expand its contents. You should be able to at least see what the class files are, and the javadocs may be bundled with it (unlikely these days but possible).
However, if you just want to know what methods are available, probably the best way is to add the JAR to the class path of a project in NetBeans, Eclipse, or IntelliJ and use their code completion features to figure out the API methods and classes.
you can do even a decompile of the jar and see the code that is behind each class using a decompiler.
A good one i found: Java Decompiler
JD-GUI home page: http://java.decompiler.free.fr
It does really good it's job. At least in the tasks i had.
Related
I'm facing some issues with jasper and I need to try to edit SmapUtil class inside jasper.jar file
However, I'm facing some problems to do so.
I've used jd-gui to decompile the jasper.jar file, took out the SmapUtil.java file, changed the
install method from
static void install(File classFile, byte[] smap) throws IOException {
File tmpFile = new File(classFile.getPath() + "tmp");
SDEInstaller installer = new SDEInstaller(classFile, smap);
installer.install(tmpFile);
if (!classFile.delete()) {
throw new IOException("classFile.delete() failed");
}
if (!tmpFile.renameTo(classFile)) {
throw new IOException("tmpFile.renameTo(classFile) failed");
}
}
to
static void install(File classFile, byte[] smap){
File tmpFile = new File(classFile.getPath() + "tmp");
SDEInstaller installer = new SDEInstaller(classFile, smap);
installer.install(tmpFile);
while (!classFile.delete());
while (!tmpFile.renameTo(classFile));
}
it's basically to keep trying to delete the file if it doens't work the first time.
Now it's where I'm facing my problem.
If I try to compile SmapUtil.java, I face a lot of missing sources.
I've tried using javac -classpath (original)jasper.jar SmapUtil.java, but I still have a lot of sources missings.
I've downloaded a jasper-sources.jar file from god knows where and used that as a -classpath, but the missing sources remains..
How should I do that? I don't think that it should that hard to change 2 lines of a file inside a jar..
Thankss
Compiling a large project such as Tomcat can be a fairly complicated endeavour. If you try decompiling/editing/recompiling without knowing this process you will probably run into a lot of issues.
It might easier (or at least predictable) to build the entire project from sources. Once you have managed to build it, you can try editing the sources.
You should be able to checkout the sources from the project's website.
If you need to patch this class because you feel it's not working correctly, it might be worth trying submitting a bugreport
In Python the global variable __file__ is the full path of the current file.
System.getProperty("user.dir"); seems to return the path of the current working directory.
I want to get the path of the current .java, .class or package file.
Then use this to get the path to an image.
My project file structure in Netbeans looks like this:
(source: toile-libre.org)
Update to use code suggested from my chosen best answer:
// read image data from picture in package
try {
InputStream instream = TesseractTest.class
.getResourceAsStream("eurotext.tif");
bufferedImage = ImageIO.read(instream);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
This code is used in the usage example from tess4j.
My full code of the usage example is here.
If you want to load an image file stored right next to your class file, use Class::getResourceAsStream(String name).
In your case, that would be:
try (InputStream instream = TesseractTest.class.getResourceAsStream("eurotext.tif")) {
// read stream here
}
This assumes that your build system copies the .tif file to your build folder, which is commonly done by IDEs, but requires extra setup in build tools like Ant and Gradle.
If you package your program to a .jar file, the code will still work, again assuming your build system package the .tif file next to the .class file.
Is there a way to get the file path of the .java file executed or compiled?
For completeness, the literal answer to your question is "not easily and not always".
There is a round-about way to find the source filename for a class on the callstack via StackFrameElement.getFileName(). However, the filename won't always be available1 and it won't necessarily be correct2.
Indeed, it is quite likely that the source tree won't be present on the system where you are executing the code. So if you needed an image file that was stashed in the source tree, you would be out of luck.
1 - It depends on the Java compiler and compilation options that you use. And potentially on other things.
2 - For example, the source tree can be moved or removed after compilation.
Andreas has described the correct way to solve your problem. Make sure that the image file is in your application's JAR file, and access it using getResource or getResourceAsStream. If your application is using an API that requires a filename / pathname in the file system, you may need to extract the resource from the JAR to a temporary file, or something like that.
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(getPackageParent(Main.class, false));
}
public static String getPackageParent(Class<?> cls, boolean include_last_dot)
throws Exception {
StringBuilder sb = new StringBuilder(cls.getPackage().getName());
if (sb.lastIndexOf(".") > 0)
if (include_last_dot)
return sb.delete(sb.lastIndexOf(".") + 1, sb.length())
.toString();
else
return sb.delete(sb.lastIndexOf("."), sb.length()).toString();
return sb.toString();
}
}
I've built a Java application that loads an image at runtime. The location of the image is fixed relative to the project.
I would like to be able to run the program from both within Eclipse and the command line and for it to load the image correctly. However, I can only do one or the other but not both. This seems like such a trivial thing to want to do but I can't find out how to do it.
The project is set up so that it creates a bin directory for the output and puts the image in a resources sub-folder. This is fine when running from the command line as I can write my code to look in that sub folder for the file.
But when I run the program from within eclipse the current working directory is different.
What am I missing?
TIA
Update - adding some code
This is what I had originally:
BufferedImage awtImage = ImageIO.read(new File(System.getProperty("user.dir") + "/resources/image-name.png"));
Following the advice in the comments I am trying to use getResourceAsStream but I don't know what to pass to the File constructor.
InputStream temp = MyClass.class.getResourceAsStream("resources/image-name.png");
BufferedImage awtImage = ImageIO.read(new File(???));
The resource is being found because temp is not null.
I think there's 2 solutions.
1) you specify an absolute path
2) your image is in the classpath so you could load it via :
YouClass.class.getResourceAsStream("YourImg.png");
The working directory, if that's really what you mean, is not a great place to load an image from. It appears that you have an image that you would distribute with your finished program so that the program could use it. In that case, I suggest that you use Class.getResourceAsStream(), and put the image in the directory with (or near) that class.
EDIT:
Here is code I used in one of my programs for a similar purpose:
ImageIcon expandedIcon = null;
// ...
expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.png"));
The ImageIcon class is part of Swing; I don't know if you're using that, but this should serve to show you the idea. The getResource() method takes a URL; again, you might need something a little different. But this shows the pathname relative to the path of the class on which the method is called, so if TreeIcon is in x/y/z/icons, the PNG file needs to be in x/y/z/icons/images, wherever that is on that computer.
TreeIcon is a class of mine, and its internals will not help you, so I'm not posting them. All it's doing here is providing a location for the PNG file I'm loading into an ImageIcon instance.
In addition to working on a disk with a directory structure, this also works in a jar file (which is a common way to distribute a java program or library). The jar file is just a zip file, and each file in the jar/zip file has its directory associated with it, so the image can be in the jar in the correct directory just as the java classes are in their directories.
getResourceAsStream() returns a stream; if you want to use that byte stream to load as an image, find a class that converts an stream to something your image class can use as a constructor or in a load method and hook them up. This is a common thing to have to figure out with Java i/o, unfortunately there is no cookbook way to do it across all images and situations, so we can't just tell you what it is.
EDIT 2:
As from the comment, try:
ImageIO.read(new File(MyClass.class.getResource("resources/image-name.png");
I set up my Eclipse projects like this.
The input directory is added to the classpath (JavaBuildPath in Eclipse).
Finally, you access the image and / or text files like this.
private BufferedImage getIconImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"/StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
I can't seem to find a solid answer for this specific question.
I'm trying to create a symbolic link programmatically of a directory in my assets folder in another location within the same application's asset directory. Essentially, I'm looking to do the same thing as what the createSymbolicLink method of Java.nio.Files would do.
Is there an available way of doing this with the Android SDK? If not, is it possible in the NDK?
For Android API 21 and above, just use:
Os.symlink(originalFilePath,symLinkFilePath);
There is no public API to do this. You can however use some dirty reflection to create your symbolic link. I just tested the following code and it worked for me:
// static factory method to transfer a file from assets to package files directory
AssetUtils.transferAsset(this, "test.png");
// The file that was transferred
File file = new File(getFilesDir(), "test.png");
// The file that I want as my symlink
File symlink = new File(getFilesDir(), "symlink.png");
// do some dirty reflection to create the symbolic link
try {
final Class<?> libcore = Class.forName("libcore.io.Libcore");
final Field fOs = libcore.getDeclaredField("os");
fOs.setAccessible(true);
final Object os = fOs.get(null);
final Method method = os.getClass().getMethod("symlink", String.class, String.class);
method.invoke(os, file.getAbsolutePath(), symlink.getAbsolutePath());
} catch (Exception e) {
// TODO handle the exception
}
A quick Google search showed this answer if you don't want to use reflection: http://androidwarzone.blogspot.com/2012/03/creating-symbolic-links-on-android-from.html
I am making software that will need to dynamically create java bytecode, and possibly even make it a runnable jar.
My current idea is to create a new .java file and compile it at runtime. I can create the file, but I'm not sure how to compile it at runtime, and make it a runnable jar. Any help would be greatly appreciated.
public static String generate(String filePath) {
try {
File file = new File("Test.java");
if(!file.exists())file.createNewFile();
FileWriter write = new FileWriter(filePath+".java");
BufferedWriter out = new BufferedWriter(write);
out.write(
"public class Test {\n\t" +
"public static void main(String[] args){\n\t\t"+
"System.out.println(\"Working\");\n\t"+
"}\n" +
"}"
);
out.close();
} catch (Exception e) {// Catch exception if any
return "Error: " + e.getMessage();
}
return "Test.java created!";
}
To compile Java code, you can use the Java Compiler API.
To generate byte code directly, you can use tools like ASM.
To generate an executable JAR, create a JarOutputStream with a manifest that declares the main class.
You can certainly use ASM and the like, but don't discount scripting languages on the jvm. ASM might be confusing if you're not very familiar with the vm and bytecode in general. You could generate groovy or jython (or whatever language you like) code to a file, and package that up with a main program in a jar file.
As an example, you create a main program which looks for a classpath resource and sends it off to the groovy scripting language:
Binding binding = new Binding();
// maybe set some binding variables here
GroovyShell shell = new GroovyShell(binding);
GroovyCodeSource source =
new GroovyCodeSource(
Thread.currentThread().getContextClassLoader().getResource("script.groovy"));
Object value = shell.evaluate(source);
Once you have that, it would be easy to package up the main class and the script in an executable jar. You could even unpack the groovy-all.jar and pack it up in your jar to make it a fully self contained executable jar file.
It all boils down to what you are trying to accomplish, and either approach would probably work for you.