Finding pathway of a text file - java

I know there are 2 ways of writing a pathway for a file while reading file in JAVA.
//1st way
scanner = new Scanner(new File("C:\\Users\\User\\IdeaProjects\\EDSS\\src\\file\\JobList.txt"));
//2nd way
scanner = new Scanner(new File("src/file/JobList.txt"));
But when I try to convert my format from .java to .exe(Application) with the 1st way in IntelliJ via something called "Build Artifacts", I can open the application smoothly. However, with 2nd way, I cannot open the Application and I found out that it is because of the pathway. And 1st way only allow me to run the Application in my computer only. If I copy the file to my friend's laptop with different username, for example ("C:\Users\Bernard..."), the application cannot work too and I have to manually modify the pathway in the codes to run the Application.
Is there any way to search for a pathway accurately without manually type the full length out and at the same time, the Application (.exe) can work with it?

You run into the problems with absolute and relative paths.
Your first option is an absolute path. which means you will always grab the file in a particular location on the computer but if that location does not exist you can't find it
Your second option uses relative path which will look for a file based on the starting location of your program so an exe might start from a different location then the command line you build java from.
The solution is to use relative paths and to make sure the Joblist.txt file is always in one location relative to your run location.
If you want to know where the starting path of your program is you can run the following code:
System.out.println(new File(".").getAbsolutePath())

Related

how to import a text file in netbeans? [duplicate]

This question already has answers here:
Reading a .txt file using Scanner class in Java
(11 answers)
Closed 3 years ago.
I have an assignment from my teacher i have make a serial program into a parallel with OMP.It's the Barnes-Hut one and its the first time i'm using netbeans.
I have a text file with someone numbers and i gotta import it into the project so it can use the values the text file has.How can i import the example.txt into netbeans?
I've tried this but it doesn't work
File myFile = new File("example.txt");
It also contains a scanner
public static void main(String[] args) {
// for reading from stdin
Scanner console = new Scanner(System.in);
Make sure your text file is in the classpath
You have a number of choices.
1. Relative Path
You can use a relative path. This means you take your current "working" location and look for the file from there, this is basically what you're doing now.
The problem with this is, the "working" location changes, and is based on the location from which the program is executed. Remember, the location of the class is not the same as the "working" location.
Netbeans "default" "working" location is usually the root directory of the project. There are ways you can configure Netbeans to use a specific "working" location, but you need to consider what happens when you're no longer running in Netbeans.
So a "simple" solution would be too drop the file into the Netbeans project directory. A slightly more advance solution would be to configure the project's "working" directory to a location containing the file
2. Absolute Path
This is when you use a fully qualified path from the a root location to the file (ie C:\some\place).
The problem with this is, rarely are this paths the same from one computer system to another and you need to remember to place the file into this location each time.
2.1 A "well known" location
A form of the absolute path solution is to place the file into a "well known" location, typically based on the OS.
For example, on Windows, you would typically use something like {user.home}\AppData\Remote\{name of your application}.
This would allow you to place the file in, what is essentially a "static" location and load it when ever you needed to.
But, this is probably over kill for your needs
3 Embedded resource
A more common solution is to "embed" the resource with your application. Essentially, this is placing the file within the applications class path, which allows the Java runtime to locate it.
In Netbeans, this means putting the file within the src directory, preferably in a subdirectory, like resources for example.
Then, when you want to read the file, you'd need to use Class#getResource(String) or Class#getResourceAsStream(String) to gain access to it.
So, based on the available information, and the example above, you might do something like...
try (InputStream is = YourClass.getResourceAsStream("/resources/example.txt")) {
// Read the file...
} catch (IOException exp) {
// Handle the exception
}
Just remember, this renders the resource "read-only" (at least for context of this simple example)

How can I get Eclipse to find my .txt file in java ON A MAC? [duplicate]

I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".

how to define a file path in java to make it ready for production phase?

I am writing a program in java with netbeans IDE which receives a jasper report *.jrxml and then displays the report for the user. I wrote the following line of code for the file path
String reportSource = "src\\jasper-reports\\report.jrxml";
but when I move the dist folder in some other place and try to run the jar file inside it, my program can not find the report.
my problem is that were should I put the *.jrxml file and how to define it's path in my program so that when I want to give my software to someone else it runs without any errors (e.g. the program can find the file)
avoid using absolute paths. try to include the file as a resource in your netbeans project. then in your code you can search for and load the file as
new InputStreamReader((Main.class.getResourceAsStream("/report.jrxml")))
something like that depending on where the file resides in your project
it's more recommended using one of the two approaches:
pass the locations/paths as a -Dproperty=value in the Java application launcher command line http://www.tutorialspoint.com/unix_commands/java.htm
store it the locations/paths in a configurations file with a unique key and edit the file accordingly for different environments,
e.g.this files always stored in ${HOME}/config_files/ directory
absolute paths considered a bad practice

Relative/Absolute Path in Java

I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".

Java get current file name EXE

This is my first Stackoverflow question.
I searched across Google for getting the current file name in Java. Most of the sources tell users how to find the current file name if the file is a JAR file, but I'm asking for if the current file is an EXE file.
I saw one EXE answer in Get name of running Jar or Exe, but I don't think it worked.
I use the JSmooth EXE wrapper (launch4j somehow didn't work for me), and Java 8. Is there a straightforward solution to my question? Also, it's nice to explain how it works, or provide a link to the Java documentary about it.
EDIT: To clarify, let's say that I made a Java program and used a JAR wrapper, and I named the resulting EXE "test.exe". I want the Java program be able to give me the current directory of "test.exe", including the filename itself (test.exe).
ANOTHER EDIT: Just to clarify more, go onto your desktop, create a text file, and put some text in it. Save it, and change the text file to an EXE file. Then, try to open it. Windows will give an error. Notice how the title of the message dialog is the file path of the opened file. That is the type of output I want.
Thanks.
Per the JSmooth documentation,
JSmooth also makes some special variable accessible for your application.
Form Meaning
${EXECUTABLEPATH} Replaced by the path to the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable
is replaced with c:/program files/jsmooth
${EXECUTABLENAME} Replaced by the name of the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable is
replaced with test.exe
You set these in JSmooth under the "Environment Settings" (the last panel), which allows you to map the variable name. So,
MY_EXECUTABLEPATH=${EXECUTABLEPATH}
MY_EXECUTABLENAME=${EXECUTABLENAME}
In your application, you can get those with
String execPath = System.getProperty("MY_EXECUTABLEPATH");
String execName = System.getProperty("MY_EXECUTABLENAME");
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
}
}
This will print a complete absolute path from where your application has initialized. It is used to get only the DIRECTORY.
If you are using a .exe why do you not create a installer? You can use Inno Setup, this way you are able to specify where do you want to store your .exe, and get it from your application just passing your custom directory

Categories

Resources