I have a problem with my Java program. I want to delete a file on my hdd; I use a MacBook.
Here is my code:
public static void main(String[] args)
{
File actualFile = new File("/Users/luffy/test.xml");
actualFile.delete();
}
chmod is set! Help please.
Make sure that the file is not in use by another program.
Make sure that you have closed all streams accessing this file.
Related
I have a homework and I did it on Linux, Visual Studio Code, and the command line. It was working perfectly fine until I need to debug my code. So I migrated to Windows 10 because I had IntelliJ IDEA installed there. I compiled the code and place the input file "bank.in" in the same folder as the compiled "MyClass.class"
However, when I run the program from IntelliJ, my code catches the exception that it cannot find the file "bank.in" when it is just in the same folder as "MyClass.class".
My method in creating the bank.in was, right clicking the out folder from IntelliJ and adding a new file and adding the bank.in contents from there
I've tried running it through cmd.exe using java MyClass and it works perfectly. No exceptions are caught.
But when run through IntelliJ IDEA, it shows
Cannot find bank.in...
Exiting the program...
This is the part of my code where I input my file.
public void main(String[] args)
{
String fileName = "bank.in";
FileReader bank = null;
BufferedReader bankBuffered = null;
try
{
bank = new FileReader(fileName);
bankBuffered = new BufferedReader(bank);
}
catch(FileNotFoundException f)
{
System.out.printf("%s is not found.%n%n", fileName);
System.out.printf("Exiting the program...%n");
System.exit(0);
}
}
This is my project folder structure
MyProject
-.idea
-encodings.xml
-misc.xml
-modules.xml
-workspace.xml
-out
-MyClass.class
-bank.in
-src
-MyClass.java
When I run it through cmd.exe, it works fine. Is there any workaround through this? Thank you.
I wrote a java file including javaFX. Now, I want to run this file, like
java -jar example.jar
But I'd like to suppress the graphical output.
Is there any possible, like a flag or anything else, to do this?
My program normally shows a progressbar and after that a video of the simulation.
Thanks a lot.
To elaborate on JB Nizet's comment.
JAR files have manifests. In order to run your JAR using the command java -jar example.jar, the manifest must have a Main-Class entry. And your main class must have a main() method.
So launch your app like so...
java -jar example.jar NO_GUI
And in your main() method, write something like the following...
public static void main(String[] args) {
if (args.length > 0 && "NO_GUI".equals(args[0]) {
// Don't show GUI
}
else {
// Show the GUI.
}
}
You could call hide() on the scene so the window dissapears.
There is not really a way to force this.
Instead, implement it as feature. Create a command line argument nogui and react to it:
public static void main(String[] args) {
boolean useGui = true;
if (args.length > 0 && args[0].equals("nogui")) {
doNotUseGui = false;
}
// Create your program and give it the flag
Program prog = new Program(useGui);
...
}
Note that theoretically it would be possible to hack your application and remove any such calls, or to suppress the calls on a native level. But I guess that is not really the route you want to go.
I know this topic has been up a lot of times and I have been looking at stackoverflow for hours after the sollution but still not working. From Eclipse I can't make my jar-file work when exporting a runnable jar-file. The program is working perfectly in Eclipse when I run it but when I try to open the jar-file the screen is going black for a second then nothing happens. I try to open it in command with java -jar nameoffile.jar and there is filenotfoundexception. This is what it looks like in cmd when I have tried to run it.
Do anyone have any suggestions?
Thanks
The problem is how you get your resources.
You put your wav file in src folder an the try get them using FileInputStream, that's not going to work.
If you put your file into the jar you should access them throug:
getClass().getResourceAsStream(filepath)
Something like this.
public class LoadFile {
public static void main(String[] args) throws Exception {
InputStream stream =
LoadFile.class.getResourceAsStream("/songwavs/s1_..._.wav");
AudioSystem.getAudioFileFormat(stream);
AudioSystem.getAudioFileFormat(url);
}
}
That's just an example, the point is that being the resource inside the jar, the ClassLoaders are the tool to get it.
So, inside a non static method you could use:
public void myMethod() throws Exception{
InputStream stream = getClass().getResourceAsStream("/songwavs/s1_..._.wav");
AudioSystem.getAudioFileFormat(stream);
}
In some circumstance the you need to use the Thread classloader like this:
public void myMethod2() throws Exception{
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/songwavs/s1_..._.wav");
AudioSystem.getAudioFileFormat(stream);
}
Then you can put read your file both when running from eclipse or from the jar.
Much more could be said about how to package an application and deploy it, but that's just a start.
I ran into library loading problems after creating a jar from my code via maven. I use intelliJ idea on Ubuntu. I broke the problem down to this situation:
Calling the following code from within idea it prints the path correctly.
package com.myproject;
public class Starter {
public static void main(String[] args) {
File classpathRoot = new File(Starter.class.getResource("/").getPath());
System.out.println(classpathRoot.getPath());
}
}
Output is:
/home/ted/java/myproject/target/classes
When I called mvn install and try to run it from command line using the following command I'm getting a NullPointerException since class.getResource() returns null:
cd /home/ted/java/myproject/target/
java -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
same for calling:
cd /home/ted/java/myproject/target/
java -Djava.library.path=. -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
It doesn't matter if I use class.getClassLoader().getRessource("") instead. Same problem when accessing single files inside of the target directory instead via class.getClassLoader().getRessource("file.txt").
I want to use this way to load native files in the same directory (not from inside the jar). What's wrong with my approach?
The classpath loading mechanism in the JVM is highly extensible, so it's often hard to guarantee a single method that would work in all cases. e.g. What works in your IDE may not work when running in a container because your IDE and your container probably have highly specialized class loaders with different requirements.
You could take a two tiered approach. If the method above fails, you could get the classpath from the system properties, and scan it for the jar file you're interested in and then extract the directory from that entry.
e.g.
public static void main(String[] args) {
File f = findJarLocation("jaxb-impl.jar");
System.out.println(f);
}
public static File findJarLocation(String entryName) {
String pathSep = System.getProperty("path.separator");
String[] pathEntries = System.getProperty("java.class.path").split(pathSep);
for(String entry : pathEntries) {
File f = new File(entry);
if(f.getName().equals(entryName)) {
return f.getParentFile();
}
}
return null;
}
I'm trying to run project which uses fannj library, but I'm getting error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'fann_create_standard_array':
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327)
at com.sun.jna.Native.register(Native.java:1355)
at com.sun.jna.Native.register(Native.java:1032)
at com.googlecode.fannj.Fann.<clinit>(Fann.java:46)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:14)
Java Result: 1
This is what I did:
I put fannfloat.dll to C:\Windows\System32
I added fannj-0.3.jar to project
I added newest jna.jar to project
here is code:
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:\\Windows\\System32");
System.loadLibrary("fannfloat");
Fann fann=new Fann("D:\\SunSpots.net");
fann.close();
}
SunSpots.net is file from example package. fannfloat.dll: you can get from here.
The "#8" at the end of _fann_create_standard_array indicates that the library is using the stdcall calling convention, so your library interface needs to implement that interface (StdCallLibrary) and it will automatically get the function name mapper applied that converts your simple java name to the decorated stdcall one.
This is covered in the JNA documentation.
It was the first time I had to work with FANN and it took me some time to make it work.
Downloaded Fann 2.2.0. Extract (in my case "C:/FANN-2.2.0-Source") and check the path of the fannfloat.dll file. This is the library that we will use later.
Download fannj-0.6.jar from http://code.google.com/p/fannj/downloads/list.
The dll is compiled for 32 bit environment. So, make sure you have a 32 bit Java installed (even in 64 bit Windows).
I suppose you already have the .net file with your ANN. Write something like this in Java
public class FannTest {
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:/FANN-2.2.0-Source/bin");
Fann fann = new Fann("C:/MySunSpots.net" );
float[] inputs = new float[]{0.686470295f, 0.749375936f, 0.555167249f, 0.816774838f, 0.767848228f, 0.60908637f};
float[] outputs = fann.run( inputs );
fann.close();
for (float f : outputs) {
System.out.print(f + ",");
}
}
}