desktop.open in Java opens .bat file instead of a folder - java

File file = new File ("D:\\Folder\\Folder2\\");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
The following code supposed to open Folder2, but instead it opens D:\Folder\Folder2.bat file.
How to fix that?

Opening folder through Deskopt.open() would be delegated to Desktop.browseFileDirectory() (JDK 8/9)
But, as seen in JDK-8233994, that is not supported/implemented for Windows.
So the alternative with explorer.exe is indeed the recommended way:
Process p = new ProcessBuilder("explorer.exe", "/select,D:\\Folder\\Folder2").start();

The Desktop API describes the method open() clearly:
Launches the associated application to open the file. If the specified
file is a directory, the file manager of the current platform is
launched to open it.
Or see the reply of #vonC

Related

Opening External Programs using Java

I'm trying to open a new DevCPP file here. But I want to open it from Java itself and not having to go click on "New" in DevCPP.
This is what I've tried using Runtime object
Runtime runtime = Runtime.getRuntime();
String[] s = new String[] {"C:\\Program Files (x86)\\Dev-Cpp\\devcpp.exe"};
try {
runtime.exec(s);
} catch (IOException e1) {
e1.printStackTrace();
}
I want to open the devcpp.exe to a page where a new File has been created
The above program works fine for opening Dev but does not create a new file by itself.
P.S : Thanks in advance
Although I don't have DevCpp installed (no admin rights on my PC), this should do the trick:
String[] s = new String[] {"C:\\Program Files (x86)\\Dev-Cpp\\devcpp.exe", "new_file.cpp"};
If I understand your question correctly, you want to open notepad for an already existing file using java program, then the command will be like this.
notepad
An example is given below.
notepad C:\Users\Name\Desktop\Temp-Movies.txt
Use the above as command in the java program.
Before executing java program, first run using command prompt.

NetBeans Java Project from command line: Working directory is System32

If I run my Java program in NetBeans and follow the information given in the output window to run from a command line:
To run this application from the command line without Ant, try:
java -jar "C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\assignment2.jar"
The program starts to run, but when it comes to run the following code to open a .txt file (my "database"):
System.out.println("Loading database of stored transactions...");
try
{
file = new File("TransactionDetails.txt");
inFile = new Scanner(file);
}
// if the log couldn't be found in the default program location
catch (FileNotFoundException ex)
{
System.out.println(CustomMessages.FileNotFound() +
System.getProperty("user.dir")); // display default directory
System.out.println(CustomMessages.systemExit());
System.exit(1); // the program needs the log to function as intended
}
It cannot find the .txt file and prints the default directory as the Windows System32 folder. How can I specify the location to be the Project folder as expected?
You could use an absolute path to the file instead of a relative path. e.g
file = new File("C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\TransactionDetails.txt");
inFile = new Scanner(file);
You cannot rely on the current working directory to be set to anything.
Either provide the file as a class path resource instead or ask the jvm where the class is located in the file system and locate the file relative to that.
For a read only file I would consider providing it as a resource.

Android Studio code writing a local file on OSX

Running in the Debug mode, with phone connected "not emulator", this Android Studio code tries to create/write a file on the local Mac machine running El Capitan but it gives error:
java.io.IOException: open failed: ENOENT (No such file or directory)
try {
Log.d("TAG", "trying to write to file");
String path = "/Users/myName/Desktop/sss.html";
File file = new File(path);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write in file
bw.write(response);
// close connection
bw.close();
Log.d("TAG", "finish file writing");
} catch (Exception e) {
Log.e("TAG", e.toString());
}
I don't know how to create a file on the local developing machine and would appreciate some how to. Thanks
edit
As stated in the comments. If the path is pointing to a file on the phone, How can I adjust it so that it points to a file on the developing machine?
You can't make or write to a file from a phone/emulator on a devoloping machine without some form of network.
These are 2 different machines and are completely independent.
You can always make a file on the emulator and send it to your developing machine using some kind of network.

File upload from a web-based application to a linux server

It seems that my code won't work after learning that the machine that I'll be pointing the upload path to is a Linux box.
My use case is, a user logs in to the web app, chooses a file to upload, then click upload button. Is it possible to do this direct from the Java code to the Linux server using appropriate ssh or scp libraries if there is any?
EDIT: Here's my current code.
#Override
public void fileTransfer(File uploadedFile, String fileName, String pathTemp) {
File destFile = new File( pathTemp + File.separator + fileName);
try{
FileUtils.copyFile(uploadedFile, destFile);
String getTempFile = destFile.toString();
String tempPath = getTempFile.replace("\\", "\\\\");
File tempFile = new File(tempPath); // 1st file
String tempFileName = tempFile.getName();
String fileSave = getUploadPathSave().replace("\\", "\\\\");
tempFile.renameTo(new File(fileSave + tempFileName));
} catch (IOException ex) {
System.out.println("Could not copy file " + fileName);
ex.printStackTrace();
}
}
If your app is deployed at one place only (not mass distribution), the easiest way would be:
create samba share on linux machine
map samba share to logical drive on windows machine
do usual file copy with java functions.
attention: renameTo will not work between drives. You'll need to copy input stream to output stream or, better, use apache commons-io functions for that.
There are different possibilities:
If you can create a shared directory in linux and mount it under windows (see Samba. Then you can write to that directory like a local directory. File will go to the linux server.
Use a library like Jsch to upload the file from windows server to linux server.
There are certain things you can do:
1-> If you can program your linux server, then you can make a program that listens to user request on a port, and stores data in file. Then you can send you files to that port of server.
2-> The other way is you can use some sort of script to create ssh-connection to server and then you can just add file through ssh, but here your java program will not be useful.
I personally use my own program to share files between 2 machines in same network.
You can use it,if it will be useful for you: https://github.com/RishabhRD/xshare

Eclipse FileInputStream with local development and remote file

I'm developing a java web app on my local PC that uses a file on the deployment server. I have to do something like this to get it to work both locally during development and on the server when deployed. Is this standard practice? Or is there a way in Eclipse using linked folders and files to specify the file the same as it is referenced on the server?
InputStream in = null;
try {
in = new FileInputStream("/EBWEB/www/homepages/path/to/file/STKCore.properties");
} catch (java.io.FileNotFoundException ex) {
in = new FileInputStream("W:/internal/www/homepages/path/to/file/STKCore.properties");
}
W: is how I have the remote server mapped using Samba in Windows XP on my PC.
You can put your properties file in your war and open it with a getResourceAsStream().

Categories