Java Working Directory - java

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

Related

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 get my program to run outside of eclipse

I have a project in eclipse. It does everything I want it to do when I click on the green circle play button in the IDE - opens the window, plays the stuff, everything. But, try as I might, I cannot figure out how to get it to do that outside of eclipse.
My project uses the processing.core library to do some of its stuff, but I am unable to get the project to function in the Processing IDE, because of some stupid stuff about one of my classes not being a valid substitute for the type parameter for Collections.sort(List<T>). If anyone knows how I can get it to export from the Processing IDE, that would be excellent.
I need to be able to email/upload to the internet/otherwise transmit some kind of file/folder/webpage that allows the recipient to, without much technical knowledge or work on their part, view a window or something that allows them to view and interact with the program in the same way I am able to interact with the window that Eclipse launches when I press the play button at the top.
I have tried several different ways of accomplishing this, but none of them have worked. I tried exporting a runnable .jar, but it wouldn't let me include the referenced libraries. I tried a regular .jar, but I don't know how to package that up with whatever is needed to be able to view it. I've even tried using the fat jar eclipse plugin for it, too, but I ran afoul of something about being unable to find the main PApplet class for the project.
If anybody knows how to get what I want to happen, or knows of a good tutorial on how to do what I want, I would greatly appreciate any sort of assistance or guidance or anything.
Google has been unhelpful in turning up solutions to this problem, because most of the results I have found were just other people asking the same or a similar question, and then either no answer, or something I had already tried, with no indication of exactly what sort of options or settings I needed to give it in order to accomplish the task.
I am using the processing.core.jar from version 2.0b7, but I don't think that would make a difference, although I plan to try it with different versions if I can't figure out anything else.
A copy of my project folder can be found at https://www.dropbox.com/sh/1n4curhxbgi8fye/A5F6_l7xQu
All the data I have is stuff I've concatenated together from successive versions of the file at http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt
I've done a quick test on OSX and had to tweak your eclipse project a bit:
You are linking to .jar libraries using absolute path. I recommend using keeping libraries relative to the project
I ran into some memory issues and had to add a couple of compiler flags
Added a main method in Earthquaker.java to initialize as an application.
main looks like this:
public static void main(String[] args) {
PApplet.main(Earthquaker.class.getSimpleName());
}
In eclipse you can export a runnable jar via File > Export > Java > Runnable JAR file. Here are a couple of screenshots:
Personally I prefer using the "Copy required libraries into a sub-folder..." option in case I need to update some dependent .jar independent of the main application .jar.
For reference I've uploaded the modified eclipse project here(Java SDK will need to be changed). The exported runnable jar with a bash script is available here.
And here is the bash script itself which should run on Linux as well:
java -Xms128M -Xmx1024M -jar Processing-DataVisualization.jar
Pretty cool project btw.
Create a simple jar. Create a batch file and specify the class path. Save the batch file. Runnin the batch file will run the app
#ECHO OFF
set CLASSPATH=%CLASSPATH%;myjar1.jar;myjar2.jar;
java mypackage.MyClass

Exporting java project from netbeans to executable jar

Used eclipse in the past fro java coding and didn't have many issues but decided to give netbeans a blast
I'm trying to create a jar file with all dependencies as one jar and yes am familiar with the java-web-start-option and also the jar actually exists in my dist folder once built.
Problem 1
All's good and well, I can launch the jar but seems not without its lib folder which contains: AbsoluteLayout.jar + beanbindings.jars. Something I will need to figure out as I go along.
I call a few batch scripts to complete differenet jobs for me in the java program e.g. one finds memory amounts and displays in a JTextArea. When I use the program within the netbeans ide all works fine.
Problem 2
When I launch the jar file thats in the dist folder it launches just fine but cannot find the batch/bash scripts at all. Below is the path I use within the program which as I mentioned works fine in the IDE and just wondering why it cant find the same path in the jar.
I assumed when I created and built like in eclipe it would make all these commands work in the existing jar in my dist folder (bin in eclipse ofc)?
String[] filepath = {"cmd.exe", "/C", "..\\Enterprise\\src\\enterprise\\batch\\memory.bat"}
Any suggestions, thanks in advance!
Just try to change your path. Just make a flag if it's in your ide then select this path, if not then use another path.
Try this link to get the application path in java.
Get the application's path

Preparing JNA for use in Eclipse

Background:
I'm doing machine learning research, and want to use the FANN library to construct neural networks. The source code is written in C, but I need it wrapped so that I can use it with a lot of Java classes we've created.
Question:
The website provides a link to an already well received wrapper software called fannj. Its dependencies is the FANN library source code and JNA. I've never done wrapping before so JNA is brand new to me. The github homepage for the code mentions "you must set the jna.library.path system property to the path of the FANN Library". I'm currently in the process of trying to do this in Eclipse on Mac OS X. A friend of mine mentioned to me earlier that it means I have to pass the location of the FANN library as argument to the virtual machine, but he only showed me how to do this via the shell. I never actually ran this command from the shell, because I wasn't sure how it would conflict with whatever configurations I do in Eclipse later.
How do I set the jna.library.path in Eclipse? The JNA directory is huge, and I don't know where the actual executable is. My friend suggested I modify its run configuration through use of the argument tab, but like I said I don't actually know where the executable is. All I have is the large uncompressed jar file. How can I go about getting it set up in Eclipse?
You don't set in an IDE, just insert in some piece of code that is called BEFORE JNA is called, something like this:
System.setProperty("jna.library.path", "path you need");
Where System is java.lang.System. As for what path you need it must be the path that contains compiled dynamically linked FAAN library (a *dll).
Or just forget that alltogether and dump FAAN (*dlls, *so..) into Windows/system32 (or other appropriate folder on system you use) and these dll's will be on default search path.
Two allow Eclipse to use JNA, all you need to do is to put two jar files in Eclipse's Java Build Path, jna.jar and platform.jar. That's it.
To do this for an individual project, right click on the project in the Package Explorer, click Properties (at the bottom), click Java Build Path on the left, then the Add External Jar files. Browse to the directory with your JNA files and add those two files.

Eclipse - Compiling & executing program

In the past, I have used JCreator to develop my java applications. I wanted to try Eclipse and see its IDE.
On JCreator, I could just open a java file and run it in the command prompt. On Eclipse, I cannot find out how I can compile and run a java file written by someone else without first creating a project, creating a new java class and then copy and pasting the code from the file.
I cannot even figure out how to import a java file into the project.
So my questions:
1. How can I open, compile and run a java file without first creating a java project.
2. How can I import a java file into a project I already have.
3. How can I run the program in command prompt, instead of on the console that is in Eclipse, (update: to be more clear) directly from the Eclipse.
Thank you.
1/ Do not forget scrapbook page (also presented here)
In theory, you could copy a class in it, but since that class is actually encapsulated into a "main invisible" class, as an inner class, it would not support any static methods in it.
However, for a quick chunk of code, it does the trick just fine.
(from this Eclipse tutorial)
3/ Since you want it run directly from eclipse, use as indicated in this thread, an External tool
That External Tool would run C:\WINDOWS\system32\cmd.exe with working directory ${container_loc}
More details in this article "How do I open a Windows Command Prompt in my console ?".
(source: avajava.com)
That will give:
(source: avajava.com)
As far as I know, you can't
Right click project > Import
With respect to your platform, something roughly equivalent to: java <classname>. (Make sure java is in your path, otherwise use the absolute path to the it.)
A useful feature is available in Eclipse since version 3.4: you can paste the text of a Java class into a Java project (copy the content of the class, select the project or source folder, paste). This will create an appropriately named file, in the correct package. This is probably the easiest way to import a single class.
2) Use the "Import" command. Ensure the java file goes to the "src" directory.
3) One way is to export your classes to a JAR file and then run the JAR file at the command prompt (e.g. java -jar file.jar)

Categories

Resources