find file location in java.io.FileNotFoundException - java

I have this Java application and I'm using an external Jar and one of the functions is throwing a java.io.FileNotFoundException. I have the file that it's looking for but I have no idea where I'm supposed to put it. Is there any program I can use that can give me the location of the path that it's trying to look at? Thanks.

if you run the application in a debugger, most debuggers allow you to break when an exception is thrown. you could then inspect the local state of the application to determine the relevant path.
you should also probably report this as an enhncement request to the original library author (to include the file name in the thrown exception).

Don't you have to the .class file of the Java class containing that method.If yes, decompile it to view the source code. This is one such decompiler
Also, try to look for any config file like a property file that may have the path information.
You can find the current working directory from System.getProperty("user.dir") and do some hit-trial by placing the file there.

If the exception stack does not give you a hint on where it is looking for the file, and placing it in common places e.g.
in user directory
home directory
current directory etc
does not work, I guess you can decompile the jar and see where it is looking for the file

Well, I doubt it is hardcoded so this will probably not show you exactly where it is looking...but you may want to decompile the class using JAD. This may clue you in on where it is looking.

Related

Change root directory that backslash points to in Java program

I am having issue running a Java program.
There's a line of code in a jar file that opens the FileInputStream to some file from "/apps/somefile" location,
however I do not have access to the root directory.
The issue is that I am unable to change the code since it is provided in a jar file, as such is there anyway to change to root directory that "/" points to when running the java program?
Here's the line for reference:
FileInputStream fileInputStream = new FileInputStream("/apps/somefile");
The line of code you present does not do anything directly. It is Java source code, and Java is ordinarily compiled to bytecode and presented in that form to the JVM. It sounds like your jar may contain source files along with the compiled class files, which is sometimes done. If that's the case then you can simply unpack the Java sources from the jar, modify them, recompile, and make a new jar from the results.
If you don't have source then you could try decompiling, as another answer suggests. That's a bit nasty, but it probably would work.
Alternatively, what you actually ask is
is there anyway to change to root directory that "/" points to when running the java program?
In fact, there may be, depending on the system on which the code is running. You could conceivably run your program under chroot, which has precisely that effect. That's a distinctly non-trivial exercise, however, and if you don't have access to the root directory (though I'm not sure how you can do anything in that case) then you very likely do not have sufficient access to set up or use a chroot environment, either.
you need to do a decompiler to that jar. You can use : http://jd.benow.ca/
Then you need to change that class that have the InputStream and then compile again and make the Jar file.

JimFS: how to get File from Path

I started using google jimfs and I can't understand how I can get file from path. In source code I see that Path.toFile throws UnsupportedOperationException. But how then can I use it without files? For example if my application need to know if some path is folder or file.
The JSR 203 API has all the tools you need for that; and in this case, the Files class.
In spite of its name, it handles everything Path. For instance, you can use:
Files.isDirectory(thePath)
to test whether a file is a directory. But there are also other ways to test for the same thing.

How to give relative path to Runtime.exec

Hello all I am trying to run help file(.chm) from netbeans directory with the help of this code
Process proc= Runtime.getRuntime().exec("hh.exe src/MOVECG_Pro.chm");
but problem arises when I try to run outside netbeans because inside jar src folder is not there. Kindly help me how to code it properly
You have placed your chm file inside your source code directory. Clearly that directory is not there when you run the JAR. I guess you want to have a standalone JAR which contains the help file bundled within it.
Since exec delegates to the native platform to interpret the command string as something it can start a subprocess with, you can imagine that there will be trouble with transparently accessing a file buried inside the JAR archive.
So either provide the help file separately, or write code which will extract the help file from the JAR at runtime. Then pass its location to hh.exe.
Let me also make a general statement: since you are writing a Java program here, which is supposed to be platform-independent, your approach with a Windows-specific help file isn't exactly by the book.

lambdaj installation

we have downloaded jar files for lambdaj and its dependencies which are again jar files.
we do not know how to go about it. we have copied these files in the
C:\Program Files\Java\jre6\lib\ext
have set the class path in environment variales as:
variable: classpath
path: C:\Program Files\Java\jre6\lib\ext
but we do not know how to go further. we want to run some lambdaj programs.
can anyone suggest how to run lambdaj programs?
You would run a Java program that requires lambdaj in exactly the same way you'd run any other java program with an external dependency, i.e. by invoking the java executable passing in the fully-qualified name of the Main class, or the JAR with an appropriate manifest, or by deploying it in a servlet container, etc. Additionally you should be putting the LambdaJ JAR on the classpath for this invocation, not in the lib folder for your entire JVM.
What have you tried so far and why/how is it not working? Your question at the moment is a bit analogous to "I want to use Microsoft Word to view some Word documents, how do I do this?".
Update for comment 1: You said "it's not working". That doesn't help anyone address your problem as it gives no clue what you expected to happen and what you observed, only that they were different. As for where JAR files can be stored - you can put them in any directory, so long as that directory is on the classpath (or you add it to the classpath) of the Java application that runs. The canonical place to put external dependencies is in a folder called lib below the root of your project, but the important thing is that you choose somewhere consistent and sensible.
It sounds like you don't quite grok Java and classpaths yet. If you have followed some tutorials and are still stuck, ask for help to let you understand. Adding more detail to your question, including the layout of your files, the commands you issued, and the response that came back would be useful too.
If you are using Netbeans create a project and right click on the Libraries folder within the desired project. Click Add JAR/Folder...

Problem with resources location difference in eclipse and JARs

I wrote a program that is based completely on a single text file: I read the file, store the information, then search the information, etc. So, for the program to work, the file just has to be present and detectable by the program.
I use eclipse, so I put the file is in the default resources map (src/main/resources). At the start of my program I create the file:
private static File textFile = new File("src/main/resources/TEXT.TXT")
However, when I try to package my program using Maven, I get a JAR in which all class and resources files are present in the same folder; my program stops working since it cannot find the file anymore.
Any help on how to deal with this problem? I`d prefer a situation in which my program works in eclipse and as a JAR, but as a JAR only would be alright as well.
You can use ClassLoader.getResourceAsStream to load it from the classpath (or getResource to get the URL of the file).
Thread.currentThread().getContextClassLoader().getResource("TEXT.TXT")
This works as long as src/main/resources is on the classpath in eclipse. (The maven eclipse plugin includes it by default.) The file has to be in the jar file to work outside of eclipse.
Nice suggestions, this works perfect in eclipse itself: the correct location of the file is returned and I can use the file to do whatever I like.
When opening the program as a jar, there is still a problem. The getResource method returns a location that looks like the right one:
/something/something/something/something/workspace/program/target/program-0.0.1.jar!/TEXT.TXT.
However, when I convert this url to a string, use that string to create a file object and use this file object in my program, I get the following error:
java.io.FileNotFoundException: file:/something/something/something/something/workspace/program/target/program-0.0.1.jar!/TEXT.TXT (No such file or directory)
So, the getResource method does find the file, but the program somehow can't use it..

Categories