How to Load Files into My Java Application

How To Open Files With .Jar (Java) Application?

I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)

BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:

   if (args.length > 0) {
File f = new File(args[0]);
start_the_application_with_a_file(f);
} else {
start_the_application_without_a_file();
}

How to load Jar file in my program?

What you are trying to do is to load the operators (Plus etc.) dynamically. If you have the operator classes under 'D:\', the way you are loading is correct. However, your interface 'Operator' in the JAR itself is not loaded. The Plus operator depends on this interface and expects it to be loaded. Adding the operator.jar file to your class path should solve the problem.

If you want to load the 'Operator.jar' also dynamically, update the urls passing to the URLClassLoader.

URL[] urls = new URL[] {uri.toURL, new File("PathtoOperator.jar").toURI().toURL() }

This should resolve the error you are receiving.

Where do I put the txt file that I want to read in Java?

Right click the project folder and click New -> file. The file will be in the Project folder and not in the source folder.

How can I import .java file in eclipse IDE?

  • Create a new JavaProject
  • Create a new class in that java project (located in workspace)
  • Name the class the same as the .java you want to import
  • Then simply copy and paste your java code into Eclipse

Importing class/java files in Eclipse

create a new java project in Eclipse and copy .java files to its src directory, if you don't know where those source files should be placed, right click on the root of the project and choose new->class to create a test class and see where its .java file is placed, then put other files with it, in the same directory, you may have to adjust the package in those source files according to the new project directory structure.

if you use external libraries in your code, you have two options: either copy / download jar files or use maven if you use maven you'll have to create the project at maven project in the first place, creating java projects as maven projects are the way to go anyway but that's for another post...

How to open a file in the same directory as the .jar file of the application?

To find the path of your jar file being executed, java.class.path is not the right property. This property may contain more than one file, and you cannot know which is the right one. (See the docs.)

To find the path of the correct jar file, you can use this instead:

URL url = MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

Where MainClass the main class of your tool, or any other class in the same jar file.

Next, the parent File of a file is its directory. So the parent File of /usr/tool/dist/tool.jar is /usr/tool/dist/. So if you want to get to /usr/tool/files/file.txt, you need to get the parent of the parent, and then from there files/file.txt.

Putting it together:

File jarFile = new File(MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
File file = new File(jarFile.getParentFile().getParent(), "files/file.txt");

How do I load a file from resource folder?

Try the next:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");

If the above doesn't work, various projects have been added the following class: ClassLoaderUtil1 (code here).2

Here are some examples of how that class is used:

src\main\java\com\company\test\YourCallingClass.java
src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java
src\main\resources\test.csv
// java.net.URL
URL url = ClassLoaderUtil.getResource("test.csv", YourCallingClass.class);
Path path = Paths.get(url.toURI());
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// java.io.InputStream
InputStream inputStream = ClassLoaderUtil.getResourceAsStream("test.csv", YourCallingClass.class);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
for (String line; (line = reader.readLine()) != null;) {
// Process line
}

Notes

  1. See it in The Wayback Machine.
  2. Also in GitHub.


Related Topics



Leave a reply



Submit