My class cannot find a python file in the jar - java

I have a jar file that launches a python script. It works if my jar file is at the root of the working directory. If I move it out of the working directory, it cannot find the python script anymore. Why does my call to find the script file not look inside the jar? I use ./(the path). Why does it automatically look in the working directory? I thought it would be self-contained.

Reposting my guess as an answer:
Are you looking for the python script as a file? or as a resource? You might want to share more information about your problem because right now all anyone can do is guess. My guess is that you're trying to read the Python script as a file, and files don't exist inside of jars. To solve this, either read the Python script as a file outside of Java, or look for it as a resource inside of the jar. If the latter, I'm not sure if you can run the script or not.
For more, please see this link: stackoverflow: can-i-make-a-jar-execute-a-python-script
Another clever option: stackoverflow: how-do-you-invoke-a-python-script-inside-a-jar-file-using-python

Related

Ship and call a .exe from within a .jar [duplicate]

I have a huge JAR file, which I created with Maven Shade plugin.
I run this with java -jar foo.jar , and my UI opens. Now I want to execute *.exe file, which is also in that JAR file, how can I do this?
I tried putting the exe to my classpath and run it from there, but after trying I found out that classpath is actually the location, where my JAR is.
Any suggestions?
Found this thing here, but is this really the best solution? Seems like alot of work, I think I have different case here, because I can define the location of exe myself and the JAR is created by me.
run exe which is packaged inside jar
Why I need this?
I want to give user a single file executable, which he can run, but my program is using the *.exe. Should I put the exe next to my jar and there will be 2 files or there is solution for my requirements?
Copying the file to a temporary location and running it is the way to go. The answer you linked to does much more work that necessary, as you can get your exe file as an InputStream and copy it to a file with a utility like Apache Commons IO FileUtils.copy(in, out)
See How do I copy a text file from a jar into a file outside of the jar? for example.
It's not about the location, it's about the fact that you need to tell your OS to run the exe and, unfortunately, you can't do that by providing a location within a jar.

How to properly make jar archive to run Java program on different systems?

I have this, perhaps, easy problem, but I don't know how to handle this.
I have my Java program and it works pretty well when I call it via terminal (Java command).
The program uses 4 text files from the hard disk which can't be added as resources to the project.
How to do this right so I could build jar file only with main class and files from hard disk (first one is a config file and it has paths to other files so the program knows where they are)?
I'm using IntelliJ IDEA 14.1.4 on Arch Linux.
I did it based on this blog, but it's not working without txt files in src folder.
Also "jar cvf" command builds jar file, but it's not working outside my computer (for example on windows or OSX).
Can anyone help me?
I prefer step by step instruction so I would understand what is going on in here.
I recommend to build your application with Maven and create a Maven Assembly which contains your JAR file as well as the config.txt file.

How to build a .jar file

Please note I do not mean building a .jar file using an IDE such as eclipse.
What I'd like to accomplish is creating a .jar using my program. For example the user enters some information and then click "Build" and a .jar file would be built and would have that information that the user enters.
I was thinking about maybe saving a file with a .java extension and writing lines of code to that file and then use cmd to build a .jar file.
That's not really the solution I want to use as I'm sure there is a better way to do it.
Take a look at
https://github.com/Quillion/Engine/blob/master/jarIt.bat
This is what I use to pack my jars. You can also learn more about it by reading documentation

Java Working Directory

I'm pretty new to programming, so this should be an easy one. I'm switching from eclipse to netbeans and I am trying to add libjinput-osx.jnilib to my working directory. I have step by step instructions for eclipse, but not netbeans. I'm about 2.5 hours and 65 google searches in and I still cant find the answer to these two basic questions I have.
What exactly is a working directory in java?
How do you add a .jnilib file to your working directory in netbeans?
My end goal is to get an xbox controller to control a game of snake I wrote. I am trying to use JInput and this tutorial. In order to compile properly on OSX I need to put libjinput-osx.jnilib in the "working directory".
The "working directory" is the location where the application is working in...cryptic huh ;)
Typically it's the launch location for the app, although this can be modified through flags and other process.
In Netbeans, the working directory is typically the root directory of the project (which contains the src and nbproject folder), but this can be changed via the project properties.
One of the simplest ways to find the working directory at run time (this is useful for testing) is to do some thing like...
System.out.println(new File(".").getAbsolutePath());
There are two aspects to this question.
When you run a command from the command line, the "working directory" is the directory you were in when you ran the command. The instructions that you are reading say to put the native library there because Java's default search path for native library includes the current directory. Alternatively, you could add a -Djava.library.path=... option to the java command to set the search path explicitly.
When you run a command from within Eclipse ... or NetBeans ... the IDE's launcher will set the appropriate JVM parameters to include the project's native library directory on the search path. You need to put the library in THAT directory. This wiki page explains what you need to do for NetBeans.
It looks like what you're really trying to do is load a native library. Try this:
public class MainClass {
static {
System.loadLibrary( "Your_native_lib_file_name" ); // Note: do not include the file extension!
}
}
You might also try -Djava.library.path=/exact/path/to/dir/
Answer copied from here:
Java can't seem to find my native libraries

How to open the executable file in this Java Project? I tried opening all the .jar files already

How to open the executable file in this Java Project? I would like to play this game. I tried opening all the .jar files already, but I get "Failed to load Main-Class manifest attributes from" the file
https://github.com/lisaglendenning/softquor
The executable is at /dist/softquor.jar
As others have said already, this jar is not a standalone App. It's actually an client server application.
I took a quick look, you can exercise the client like this:
Assuming softquor.jar is in your current working directory.
In one console, start the server
java -cp softquor.jar softquor.Server 8080
In another console start the Text client (you can start multiple clients!)
java -cp softquor.jar softquor.TextClient localhost 8080
From the class name I do say the TextClient will let you enter some command and play the game.
If you get something like ClassNotFoundException then it means the project is using some third party jar file. Unfortunately you will have to find out those dependencies yourself since there is no clear documentation on that.
I would assume the file in /dist/softquor.jar is the executable file.
You can explore contents of a JAR file just like any other zip files using a winzip. Inside you will find a /META-INF/ folder which will have the entry class.
From command prompt you can run it just like any other .jar file. Just make sure you use the full package name.
The softquor.jar isn't executable. There is no Main-Class defined in the jar manifest. I looked around briefly and did not see an obvious main entry point that wasn't in a test class. Though there was an a class that extended JApplet so you could possibly run the jar from a webpage using the java plugin.
Tough to tell as there appears to be no documentation for this project other than a short README file.

Categories

Resources