I have calling display method from dll file This works when I run my java project . But it stop working after creating of project exe file. My Code is as follow ..
static {
try {
Bridge.setVerbose(true);
try {
Bridge.init();
} catch (IOException e) {
e.printStackTrace();
}
File dll_File = new File("helloworld.j4n.dll");
Bridge.LoadAndRegisterAssemblyFrom(dll_File);
helloworld.Hello.display(str)
} catch (Exception exception) {
exception.printStackTrace();
}
}
Have you signed the dll? Please sign the dll and then check.
Related
I want to rename a csv file in java using following code segment, but file is not getting renamed.
public static void main(String[] args) {
File fileToBeRenamed = new File("C:/abc/a.txt");
File newFileName = new File("C:/abcd/b.txt");
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isRenamed = fileToBeRenamed.renameTo(newFileName);
if(isRenamed)
System.out.println("File renamed successfully");
else
System.out.println("File could not be renamed");
}
Its not throwing any error. but file is not getting renamed.So please help me to do so.
let's suppose you have a file A(fileToBeRenamed) and you want to rename it to B(newFileName). Then , no need to create "newFileName" file. your code is fine , except the file creation part.
so comment out the lines:
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it will work.
Note: I don't think it has anything to do with file extension(csv/text etc), when both are the same.
I think you want to rename a.txt to b.txt, So you don't need create b.txt. If you remove newFileName.createNewFile() will work
I am running a jar file on a different path on the system programmatically. One of the arguments needed is the path to a file, and the file is in my jar file. How can I pass the file (script.ospt) as an argument for another java application?
Current code:
Process ps = null;
try {
ps = Runtime.getRuntime().exec(new String[]{"java","-jar",engine.getAbsolutePath(), script3.getAbsolutePath()});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT: More explanation:
I have a command I would like to run from java, using Runtime.getRuntime().exec().
The command I am running requires one argument: the path to a file. If the file is in the same package as my main class, how can I use the file in my program as an argument for the command?
My program is designed to launch from a runnable jar file, set everything up if needs be, and then load a class in another jar file to initiate the program. This allows for self updating, restarts, etc. Well, the class loading code I have seems a bit funky to me. Below is the code I am using to do load the program. Is this incorrect use or bad practice?
try {
Preferences.userRoot().put("clientPath", Run.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString()); //Original client location; helps with restarts
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
Preferences.userRoot().flush();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
File file = new File(path); // path of the jar we will be launching to initiate the program outside of the Run class
URL url = null;
try {
url = file.toURI().toURL(); // converts the file path to a url
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL[] urls = new URL[] { url };
ClassLoader cl = new URLClassLoader(urls);
Class cls = null;
try {
cls = cl.loadClass("com.hexbit.EditorJ.Load"); // the class we are loading to initiate the program
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
cls.newInstance(); // starts the class that has been loaded and the program is on its way
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
The biggest problem you have is that when you get an Exception you pretend that logging the exception makes it ok to continue as if nothing happened.
If you aggregate the try/catch blocks, your code will be much shorter and easier to read, and it won't assume that exceptions don't really matter.
Try this example
public static Object load(String path, String className) {
try {
URL url = new File(path).toURI().toURL();
ClassLoader cl = new URLClassLoader(new URL[] { url });
return cl.loadClass(className).newInstance();
} catch (Exception e) {
throw new IllegalStateException("Unable to load "+className+" " + e);
}
}
I'm trying to open a pdf located in a ressource folder with my application.
It does work on the emulator but nothing happens when I try on the exported application.
I'm guessing I'm not using the rigth path but do not see where I'm wrong. The getRessource method works very well with my images.
Here is a code snippet :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported()) {
try {
URL monUrl = this.getClass().getResource(pdf);
File myFile = new File(monUrl.toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm referring to the pdf variable this way : "name_of_the_file.pdf"
Edit: I've pasted the whole method
Ok, solved it. The file being located in a Jar, the only way to get it was through a inputsteam/outstream and creating a temp file.
Here is my final code, which works great :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported())
{
InputStream jarPdf = getClass().getClassLoader().getResourceAsStream(pdf);
try {
File pdfTemp = new File("52502HPA3_ELECTRA_PLUS_Fra.pdf");
// Extraction du PDF qui se situe dans l'archive
FileOutputStream fos = new FileOutputStream(pdfTemp);
while (jarPdf.available() > 0) {
fos.write(jarPdf.read());
} // while (pdfInJar.available() > 0)
fos.close();
// Ouverture du PDF
Desktop.getDesktop().open(pdfTemp);
} // try
catch (IOException e) {
System.out.println("erreur : " + e);
} // catch (IOException e)
}
}
You mentioned that it is running on Emulator but not on the application. There is high probability that the platform on which the application is running does not support Desktop.
Desktop.isDesktopSupported()
might be returning false. Hence no stack trace or anything.
On Mac, you can do:
Runtime runtime = Runtime.getRuntime();
try {
String[] args = {"open", "/path/to/pdfFile"};
Process process = runtime.exec(args);
} catch (Exception e) {
Logger.getLogger(NoJavaController.class.getName()).log(Level.SEVERE, "", e);
}
I'm trying to develop one java function to retrieve the id of one specific folder over Google Docs, but the documentation over this language is poor and have problems to develop it. Until now I've can make this, but don't work, it never show my the document id of my folder
public void findfolder(String folderName) throws MalformedURLException,IOException,ServiceException{
URL feedUrla=new URL("https://docs.google.com/feeds/default/private/full/-/folder");
DocumentQuery query = new DocumentQuery(feedUrl);
query.setTitleQuery(folderName);
query.setTitleExact(true);
DocumentListFeed feed=null;
try {
feed = client.getFeed(query, DocumentListFeed.class);
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
for (DocumentListEntry entry : feed.getEntries()) {
System.out.println(entry.getDocId());
}
}`