Is it possible to access resources inside a jar through their path? - java

I have a SoapUI project running through a java project that is all packaged into a jar. The project runs fine unpackaged through my IDE, but it encounters problems when running the jar through the command line. Here is the code that is causing the problem:
SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
ClassLoader cl = MyClass.class.getClassLoader();
runner.setProjectFile(cl.getResource("project.xml").getFile());
runner.run();
The problem occurs on the run method when it tries to read in the xml file from the jar. Is it possible for me to get a path that I can input to the setProjectFile method from the jar? I've seen other answers say to use a stream, but this method only accepts strings.
Here is the stack trace:
2014-07-23 12:03:58,384 ERROR [errorlog] com.eviware.soapui.support.SoapUIException: Failed to load project from file [file:/C:/temp/MyJar.jar!/project.xml]
com.eviware.soapui.support.SoapUIException: Failed to load project from file [file:/C:/temp/MyJar.jar!/project.xml]
at com.eviware.soapui.impl.wsdl.WsdlProject.loadProject(WsdlProject.java:315)
at com.eviware.soapui.impl.wsdl.WsdlProject.(WsdlProject.java:231)
at com.eviware.soapui.impl.wsdl.WsdlProjectFactory.createNew(WsdlProjectFactory.java:41)
at com.eviware.soapui.impl.wsdl.WsdlProjectFactory.createNew(WsdlProjectFactory.java:28)
at com.eviware.soapui.tools.SoapUITestCaseRunner.runRunner(SoapUITestCaseRunner.java:329)
at com.eviware.soapui.tools.AbstractSoapUIRunner.run(AbstractSoapUIRunner.java:188)
at CommercialTests.ComTest.doTest(ComTest.java:14)
at CommercialTests.ServiceTests.main(ServiceTests.java:8)
Caused by: java.io.FileNotFoundException: C:\temp\MyJar.jar!\project.xml (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.handleFile(UrlWsdlLoader.java:180)
at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.load(UrlWsdlLoader.java:116)
at com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader.load(UrlWsdlLoader.java:96)
at com.eviware.soapui.impl.wsdl.WsdlProject.loadProject(WsdlProject.java:297)
... 7 more

For something like this I normally do:
URL res = getClass().getResource("/project.xml") // is it at the root of your jar?!?!
File f = new File(res.getFile())
runner.setProjectFile(f.getCanonicalPath());
SoapUI, depending on how it accesses the file, might need it in the "real" world. Below is a much more resource-intensive version of the above.
// locate your file in the jar resources
URL res = getClass().getResource("/project.xml") // is it at the root of your jar?!?!
// locate the resource in the filesystem
File f = new File(res.getFile())
// copy the file out into the real filesystem
File target = new File(System.getProperty("java.io.tmpdir") + File.separator + f.getName())
java.nio.file.Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING)
runner.setProjectFile(target.getCanonicalPath())

Related

Java getResource does not find file after building artifact in Intellij

I have used
File file = new File(Game.class.getResource("Tiles.txt").getFile())
to get the txt file from my resources folder and it works fine when inside the IDE but when building to a jar and running outside of the environment it throws file not found errors (which i saw through running in CMD).
I use a similar method to get all my images and sprite sheets:
BufferedImage loadedImage = ImageIO.read(Game.class.getResourceAsStream("EG.png"));
how do they differ in importing files and why is my path incorrect?
Error CMD gives:
http://imgur.com/a/1SC1L
C:\Users\Taka\Desktop>java -jar ProjectC-Revamped.jar
java.io.FileNotFoundException: file:\C:\Users\Taka\Desktop\ProjectC-Revamped.jar!\Tiles.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Tiles.<init>(Tiles.java:16)
at Game.<init>(Game.java:91)
at Game.main(Game.java:192)
java.io.FileNotFoundException: file:\C:\Users\Taka\Desktop\ProjectC-Revamped.jar!\Maps\Map.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Map.<init>(Map.java:20)
at Game.<init>(Game.java:94)
at Game.main(Game.java:192)
use getResourceAsStream() instead of getResource(): see this SO answer for details
URL.getFile() does not convert a URL to a file. It merely returns the path and query portions of the URL. The method is named getFile() only because the URL class was part of Java 1.0, which was released in 1995, back when most URLs happened to point to actual files.
The path returned by URL.getFile() returns a String which may or may not be a valid file path. Many characters may not legally appear in URLs, so they will be percent-escaped. A good example is spaces; for example, file:///C:/Program%20Files/Java.
In short, converting a URL to a file using getFile() is not safe and will eventually fail.
Also, an entry in a .jar file is not a file. A .jar file is a single archive file. Its entries are just subsequences of bytes (representing compressed data). On the other hand, the URL returned by getResource is always valid (assuming it’s not null), even if it represents a .jar entry.

Java 3d error when running application from .jar

Hello when i am running my application from Eclipse it runs perfectly but when i export it as a runnable jar, it doesn't run.
When i try running it from cmd it gives me one of 2 errors the first errror is after i just export it with the "copy required libraries into a sub-folder":
Exception in thread "main" java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:180)
at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:137)
at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:948)
at javax.media.j3d.VirtualUniverse.<clinit>(VirtualUniverse.java:280)
at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3862)
at Main.Game.<init>(Game.java:39)
at Main.Main.main(Main.java:6)
when i change the sub-folder's name from IslandDomination_lib to just lib it gives me another error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/Canvas3D
at Main.Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: javax.media.j3d.Canvas3D
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I have tried running it several ways:
1.
java.exe -jar IslandDomination.jar
2.
java.exe -cp "lib/j3dcore.jar;lib/j3dutils.jar;lib/vecmath.jar" -jar IslandDomination.jar
3. using another java program to run it for me:
package main;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home")+"\\bin\\java.exe", "-cp" , "\"lib/j3dcore.jar;lib/j3dutils.jar;lib/vecmath.jar\"" , "-jar" , "IslandDomination.jar");
pb.directory(new File("./"));
Process p = pb.start();
p.waitFor();
}
}
The internal structure of my jar consists of 4 folders:
J3DBool
Main
Maths
META-INF
The folder with the required jars is in the same folder as the main jar
You use a completely obsolete version of Java3D, which is very difficult to bundle in any software as you have to set the Java library path somehow and you have to bundle both the Java libraries and the native libraries. Rather rebuild your project with Java3D 1.6.0 pre 12, it's a lot easier to use as this version relies on JOGL 2 which uses automatic native library loading and everything is packaged as JARs, there is no longer any need of modifying the library path, just take care of the classpath.
Please follow my tutorial, especially the very last section with (a lot) more information.
Finally, the obsolete version that you use isn't guaranteed to work on any recent operating systems. If it works, you'll be lucky; if it doesn't, nobody will fix it.
I think the required library file (jar file) not found in the classpath.
you need to set Library file in your class path.
step to set class path:-
MyComputer->Properties->Advanced system setting->Environment variables
find "classpath" either in user variables area or in system variables if found edit that and put all the jar file link. if not found then create new(click on new) and put all the jar link there and try to run your application
I found out how to fix my problem and it is done by simply putting the needed dll files next to the executable jar file

Unable to call dll from Oracle

I have a c# DLL, i convert that dll from JNI4net to work with java. I am able to call the dll in java but when i create a jar file and trigger java function from oracle on button event. It throws exception.
Java code:
Bridge.setVerbose(true);
Bridge.setDebug(true);
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("ECR.j4n.dll"));
ComECR test = new ComECR();
test.VFI_DoSetup();
ORACLE Exception :
java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.<init>(Unknown Source)
at net.sf.jni4net.CLRLoader.findDefaultDll(CLRLoader.java:54)
at net.sf.jni4net.Bridge.init(Bridge.java:31)
at com.ecr.test.Program.getProperty(Program.java:57)
at oracle.forms.handler.UICommon.onGet(Unknown Source)
at oracle.forms.engine.Runform.onGetHandler(Unknown Source)
at oracle.forms.engine.Runform.processMessage(Unknown Source)
at oracle.forms.engine.Runform.processSet(Unknown Source)
I have signed jar and also added the jar in class path and formsweb.cfg
need help on this
You have this very message error in SO: URI scheme is not "file"
From your error, I would try the below:
Bridge.setVerbose(true);
Bridge.setDebug(true);
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("file:<fullpath>/ECR.j4n.dll"));
ComECR test = new ComECR();
test.VFI_DoSetup();
The fact that you're getting the error with Oracle only could mean Oracle doesn't work with same Path as Java alone. It isn't missing the jar file's path but the dll one's.

Writing output of dex2jar to other folder

I am trying to write a batch file that writes the classes-dex2jar.jar to some other folder.
I tried the following:
d2j-dex2jar %arg1% -o %arg2%
Here, arg1 is the path to classes.dex and arg2 is the folder where I wish to store the generated classes-dex2jar.jar file. (E:\new\)
Now, I am getting FileNotFoundException for the second argument. It says
java.io.FileNotFoundException: E:\new" (The filename, directory name,
or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:77)
at com.googlecode.dex2jar.v3.Dex2jar.to(Dex2jar.java:250)
at com.googlecode.dex2jar.tools.Dex2jarCmd.doCommandLine(Dex2jarCmd.java
:110)
at com.googlecode.dex2jar.tools.BaseCmd.doMain(BaseCmd.java:174)
at com.googlecode.dex2jar.tools.Dex2jarCmd.main(Dex2jarCmd.java:34)
Please tell me what am I doing wrong?
Instead of passing the folder as an argument, pass it the jar filename.
d2j-dex2jar classes.dex -o "E:\new\classes.jar"
Also make sure E:\new directory exists. Dex2jar does not create a new directory. Create a directory and then pass d2j-dex2jar the full path to the directory plus the jar file name you want.
java.io.FileOutputStream.open - looks like you'are trying to open directory with FileOutputStream , which should be not possible.

Java can't find my file even though I'm pretty sure I have the directory right

I'm writing a java program for class that reads in from a txt file. I placed the text file in the package called Assignment3pckg and use a scanner like so:
Scanner s = new Scanner( new File("./src/Assignment3pckg/studentdata.txt") );
But it just keeps giving me
java.io.FileNotFoundException: .\src\Assignment3pckg\studentdata.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Assignment3pckg.TestHW3.readStudentDataFromFile(TestHW3.java:25)
at Assignment3pckg.TestHW3.main(TestHW3.java:14)
Any insight would be greatly appreciated!
You are using a relative path (as opposed to an absolute path) for your file. Relative paths are resolved relative to the current working directory of the Java process when you run it.
Where this directory is depends on how you run Java.
You can find out by printing it:
System.out.println(new File(".").getAbsolutePath());
You can resolve it by:
Making your path relative to the correct directory
Using an absolute path (on Windows, that would start with C:\ or another drive letter)
Note though that in Java, you have to escape backslashes in a String. So C:\myproject\src\Assignment3pckg\studentdata.txt becomes "C:\\myproject\\src\\Assignment3pckg\\studentdata.txt" as a Java String. Or "C:/myproject/src/Assignment3pckg/studentdata.txt" as Windows doesn't mind forward slashes instead of backslashes either.

Categories

Resources