I'm trying to access a class inside of a jar using URLClassLoader.
Here is my hierarchy:
Why isn't:
URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL(
"http://rune-shadows.com/runeshadows.jar") });
applet = (Applet) classLoader.loadClass("run/Run").newInstance();
working?
I tested the same code with a different jar with no folders, just a "Main" class in the root folder and it worked fine. How is the path I have above incorrect?
Try including the www. at the start:
"http://www.rune-shadows.com/runeshadows.jar"
Related
I have an Portable Electron App (packed with: electron-builder + asar, portable build) on Windows. I try to get the application path but it returns a path within the user\temp folder rather than the actual '.exe' file
Is there any way to get the original app.exe path?
I've tried the following:
app.getAppPath()
__dirname
require.main.filename
app-root-path
and a few node modules
The path I'm getting from my tests:
C:\Users\xxx\AppData\Local\Temp\xxxxxx.tmp\app
the actual .exe Path (where the app launched from, and what i need):
C:\Users\XXX\Documents\test\dist
I'm just starting with Electron.
I found a solution:
Use the Environment Variable (created by Electron-Builder)
process.env.PORTABLE_EXECUTABLE_DIR
to show the real Path of the App.exe.
Works only packed with Electron-Builder
From the main process:
// If not already defined...
const { app } = require ('electron');
const path = require ('path');
let execPath;
execPath = path.dirname (app.getPath ('exe'));
// or
execPath = path.dirname (process.execPath);
From a renderer process:
// If not already defined...
const { remote } = require ('electron');
const path = require ('path');
let execPath;
execPath = path.dirname (remote.app.getPath ('exe'));
// or
execPath = path.dirname (remote.process.execPath);
I had a lot of trouble with this and was finally able to solve the issue by replacing __dirname by '.', see working example below :
const path = require('path')
const myAppPath = path.resolve('.', 'myapp.exe');
Seems like PORTABLE_EXECUTABLE_FILE only works under certain Electron-Builder configurations; In particular, it won't work in apps deployed as a portable directory.
In such cases, the following will get you the application root path:
const path = require('path')
import { app } from 'electron'
let rootDir = app.getAppPath()
let last = path.basename(rootDir)
if (last == 'app.asar') {
rootDir = Path.dirname(app.getPath('exe'))
}
None of the above answers worked for me on Windows 10.
process.env.PORTABLE_EXECUTABLE_DIR returns the Temp directory path.
I got it to work using:
process.env.INIT_CWD
process.env.PORTABLE_EXECUTABLE_FILE
will give you the full path to the file.
As none of the methods above worked and I don't want to use an external lib for this, i tried following:
if (!fs.existsSync("../images")) {
fs.mkdirSync("./../images");
}
return path.resolve("./../images/") + "/";
You can use any directory that should exist at the top level or anywhere else. In my case I know that one level higher in the directory there must be a directory called "images".
This solution worked in my case for DEV build and prod (packaged).
Are there any drawbacks when using this approach?
I've been developing an Grails application that uses some FileResources located in this directory:
MyGrailsApplication\web-app\images
I'm able to get this directory through grails naturally through GrailsResourceUtils.WEB_APP_DIR. However, when I convert my Grails project into a .war file and deploy it to a tomcat server, that code does not work - the path gets messed up.
Through some debug statements, I was able to find a hacky work-around to this (essentially, do a code modification right before I make the war):
// BELOW 2 LINES WORK IN THE WAR FILE, BUT NOT WITH THE RUN-APP CMD
new FileResource(new File("../webapps/InstanceChecker-0.1/images/ok.png"))
new FileResource(new File("../webapps/InstanceChecker-0.1/images/http404.png"))
// BELOW 2 LINES WORK WITH RUN-APP, BUT NOT IN THE WAR
new FileResource(new File(GrailsResourceUtils.WEB_APP_DIR + "/images/ok.png"))
new FileResource(new File(GrailsResourceUtils.WEB_APP_DIR + "/images/http404.png"))
Is there a way to cleanly get the image path both in run-app and also when its deployed as a .war? Are there some best practices that I'm missing out on here?
Edit: Perhaps I'm not putting the images in an ideal directory? I'm open to advice on that as well.
For get a resource file you can use this:
def basePath = grailsApplication.mainContext.servletContext.getRealPath('images')
Check if end with "/":
basePath = basePath.endsWith("/") ? basePath : basePath + "/"
And then:
new FileResource(new File(basePath+"fileName"))
Remember to inject grailsApplication in your controller/service
you can (also) utilize grail's linkGenerator like
final headerImage = new Image(null, new ExternalResource(Grails.get(LinkGenerator).resource(dir: "images", file: "logo.png")))
I am trying to set up Weceem using the source from GitHub. It requires a physical path definition for the uploads directory, and for a directory for appears to be used for writing searchable indexes. The default setting for uploads is:
weceem.upload.dir = 'file:/var/www/weceem.org/uploads/'
I would like to define those using relative paths like WEB-INF/resources/uploads. I tried a methodology I have used previously for accessing directories with relative path like this:
File uploadDirectory = ApplicationHolder.application.parentContext.getResource("WEB-INF/resources/uploads").file
def absoluteUploadDirectory = uploadDirectory.absolutePath
weceem.upload.dir = 'file:'+absoluteUploadDirectory
However, 'parentContext' under ApplicationHolder.application is NULL. Can anyone offer a solution to this that would allow me to use relative paths?
look at your Config.groovy you should have (maybe it is commented)
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
// "classpath:${appName}-config.properties", "classpath:${appName}-config.groovy",
grails.config.locations = [
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"
]
Create Conig file in deployment server
"${userHome}/.grails/${appName}-config.properties"
And define your prop (even not relative path) in that config file.
To add to Aram Arabyan's response, which is correct, but lacks an explanation:
Grails apps don't have a "local" directory, like a PHP app would have. They should be (for production) deployed in a servlet container. The location of that content is should not be considered writable, as it can get wiped out on the next deployment.
In short: think of your deployed application as a compiled binary.
Instead, choose a specific location somewhere on your server for the uploads to live, preferably outside the web server's path, so they can't be accessed directly. That's why Weceem defaults to a custom folder under /var/www/weceem.org/.
If you configure a path using the externalized configuration technique, you can then have a path specific to the server, and include a different path on your development machine.
In both cases, however, you should use absolute paths, or at least paths relative to known directories.
i.e.
String base = System.properties['base.dir']
println "config: ${base}/web-app/config/HookConfig.grooy"
String str = new File("${base}/web-app/config/HookConfig.groovy").text
return new ConfigSlurper().parse(str)
or
def grailsApplication
private getConfig() {
String str = grailsApplication.parentContext.getResource("config/HookConfig.groovy").file.text
return new ConfigSlurper().parse(str)
}
Hey i'm trying to localize a plugin called Donate Plus ( which locallized technicly).
the plugin came with en_CA and de_DE files, i've tried creating a he_IL file without success.
So i've tried with the de files came with the plugin but didn't work.
I've set the WPLANG in wp-config.php to de_DE yet that dosen't change the code.
this is the setting code :
load_plugin_textdomain( 'dplus', '/wp-content/plugins/donate-plus' );
And i did check that all the string are set to be localized.
Anyone has a clue?
I just was with a similar isue, did you try to rename your files from de_DE.po and de_DE.mo to name-of-plugin-de_DE.mo and name-of-plugin-de_DE.po (changing name-of-plugin with yours, of course)?
dplus-de_DE.mo and dplus-de_DE.po It must work ;)
load_plugin_textdomain takes three parameters.
In your case it would be something like this (assuming the .po and .mo files are located in a subdir called 'languages')
load_plugin_textdomain( 'dplus', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
I checked the source of DonatePlus Plugin and I found that the Plugin is doing localization wrongly.
The load_plugin_textdomain() call is made inside the DonatePlus classes constructor. But it should be present inside the 'init' hook. Trying adding the following code (which is at the of the file) inside the init function.
if( class_exists('DonatePlus') )
$donateplus = new DonatePlus();
Where are all the .po and .mo files stored? Are they inside the /wp-content/plugins/donate-plus folder itself? If not then change the path or move the files.
I had a similar issue where I was loading the translation files with the load_plugin_textdomain function from within a service class using PSR-4. This meant that the dirname( plugin_basename( __FILE__ ) ) string returned the wrong path.
The correct path is the relative path your-plugin/languages (assuming you are loading the translation files from the /languages directory).
Absolute paths such as /var/www/html/wp-content/plugins/my-plugin/languages won't work.
My plugins file structure looks something like this:
- my-plugin
- assets
- languages
- services
- Api
- Base
Translation.php
- ...
Plugin.php
- vendor
- views
composer.json
composer.lock
index.php
my-plugin.php
uninstall.php
Since my Translation service is placed in the /services/Base/ directory, this worked for me:
$root = plugin_basename(dirname(__FILE__, 3));
load_plugin_textdomain( 'my-plugin', false, "$root/languages/");
Also, I used no action hook at all instead of init or plugins_loaded and fired the load_plugin_textdomain function at the beginning of the plugin, since the hooks don't fire early enough for the admin menu and action links to get translated.
Use:
load_textdomain( TEXT_DOMAIN , WP_PLUGIN_DIR .'/'.dirname( plugin_basename( FILE ) ) . '/languages/'. get_locale() .'.mo' );
I want to use the contents of a config file in several ways, including in integration tests and in my BootStrap. If my config file is under src/groovy and is called "com.corp.MyConfig.groovy", what should I pass to the ConfigSlurper parse method?
I guess what happens is that your Groovy file gets compiled and ends up being a class in your binary directory (classpath). Instead of trying to load it via the URL try to load the script class.
Class scriptClass = getClass().classLoader.loadClass('com.corp.MyConfig')
ConfigObject config = new ConfigSlurper().parse(scriptClass)
If your config file is available on the classpath, I would suggest using ClassLoader.getResource() to get it:
URL url = MyClass.class.getClassLoader().getResource("com/corp/MyConfig.groovy");
config = new ConfigSlurper().parse(url);
From a POGO you can also use:
import grails.util.Holders
class Foo {
def bar() {
println(Holders.config.grails.serverURL)
}
}
From: How do I get at the goodies in my Grails Config.groovy at runtime?