I built a .jar file, ex. Test.jar. I open pictures in my computer by this Test.jar file.
How can I receive the absolute path of this picture?
package Run;
import Controll.Controller;
import View.MainFrame;
public class Run {
public static void main(String args[]){
MainFrame view = new MainFrame();
Controller controller = new Controller(view, fileName);
}
}
I write my project by MVC (Model - View - Control) model. The MainFrame class is the view, where i display the picture. The Controller class is the Control where control all works of my project, the constructor of Controller receive twos parameter, one is the View, and another is the fileName (name of the picture that i open by this .jar file). After receiving 2 these parameters, the Controller will display the picture in the View (a Frame).
It means, how can I receive the fileName in the Run.class to pass the parameter fileName into the Controller to work with this file?
You can pass the filename as an argument. It will be received in the args[] array in the main method.
So if you do java Run "/home/something/filename.jpg"
you can access it in the main function
public static void main(String[] args) {
String fileName = args[0];
}
Related
I inherited the maintenance work on a Springboot program that currently dumps a number of debugging logs every time the application experiences an error (which could include incorrect queries within the UI itself). These log files are never deleted by the software, so they continually build up until they eventually fill up the server computer.
I've built a utility to manage these logs by deleting them once the log files reach 7 days of age. Admittedly, I'm still familiarizing myself with Springboot, so I built this utility externally. I looked at the program structure of the software, and it only uses a single main class as I expected, but the class only contains a call to run the Springboot API in addition to a couple of beans. My question is, in general, where in the Springboot structure should I look to implement my code?
Here's what I'm seeking to add for context:
package com.climatedev.test;
//Import packages
import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;
public class CleanLogs {
//Create static array that will hold all files in the path
public static File[] files;
//Give the program the path of the log files to delete
private void getPath() {
File file = new File("C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data");
String id = "CW-LANDCAST-bin";
files = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File file, String name) {
return name.startsWith(id);
}
});
}
//Delete all files older than 7 days
private void deleteLogs() {
getPath();
for(File f : files) {
long diff = new Date().getTime() - f.lastModified();
if (diff > 7 * 24 * 60 * 60 * 1000) {
f.delete();
}
}
}
//Call the clean files program (testing purposes only)
public static void main(String[] args) {
CleanLogs obj = new CleanLogs();
obj.deleteLogs();
}
}
I am trying to load an image from my computer into a code to produce a color histogram. My code is compiling but it says that the image is not found, although it is on the Home part of my laptop as 'me.jpg.' Below is the first part of my code, Any tips?
import java.io.*;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.*;
public class test {
public test() {
}
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", "me.jpg"); // Load Image
int [][] imageHistogram = getHistogram(image);
FileWriter writer = null;
File outputFile = new File("test2.txt");
I recommend you store your code and your data (images) in different, proper, places.
Then, open the terminal and set the data directory as the current directory. And invoke the JVM specifying the code directry into the classpath:
java -classpath <directory-of-code> my.class <parameters...>
Update
Also, you could pass the absolute path as a parameter and receive it in your code:
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", args[0]);
...
And the command line:
java -classpath <directory-of-code> my.class my-home/me.jpg
I am learning and programming a game for android using libgdx and i have got stuck on this error for quite a long period of time. I have enclosed the following content
The code.
The command i used to access the asset.
Screenshot that contains the error when i debug the code on android.
Screenshot of my Package Explorer.
All the possible combinations i tried to get the code working.
1.The Code
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
public class Gameassetloader implements Disposable, AssetErrorListener {
public static final String TAG = Gameassetloader.class.getName();
public static Gameassetloader instance = new Gameassetloader();
private AssetManager assetManager;
/** Load the appropriate texture for the respective entities **/
public AssetCharacter boy;
public AssetRock rock;
public AssetShield shield;
/** singleton: prevent instantiation from other classes **/
private Gameassetloader(){}
public void init(AssetManager assetManager)
{
this.assetManager = assetManager;
//set asset manager error handler
assetManager.setErrorListener(this);
//load texture atlas
assetManager.load(Gameconstants.TEXTURE_ATLAS_OBJECTS,TextureAtlas.class);
//start loading assets and wait until finished
assetManager.finishLoading();
Gdx.app.debug(TAG, "# of assets loaded:" + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames())
Gdx.app.debug(TAG, "asset:" + a);
TextureAtlas atlas = assetManager.get(Gameconstants.TEXTURE_ATLAS_OBJECTS);
//enable texture filtering for pixel smoothing
for(Texture t : atlas.getTextures())
t.setFilter(TextureFilter.Linear,TextureFilter.Linear);
//create game resource objects
boy = new AssetCharacter(atlas);
rock = new AssetRock(atlas);
shield = new AssetShield(atlas);
}
public void dispose() { assetManager.dispose(); }
#Override
public void error(String fileName, Class type, Throwable throwable)
{
Gdx.app.error(TAG, "Couldn't load asset' " + fileName + "'", (Exception)throwable);
}
}
2.The Command
public static final String TEXTURE_ATLAS_OBJECTS ="gdxgame-android/assets/packed/packed.png";
3.Screenshot of Package Explorer
http://i1294.photobucket.com/albums/b608/Abhishek_M369/PackageExplorer_zpsd2589ee2.jpg
4.Screenshot of Error log (Android DDMS)
http://i1294.photobucket.com/albums/b608/Abhishek_M369/ErrorLog_zps8a4a68d9.jpg
5.All the possible combinations i tried
"/gdxgame-android/assets/packed/packed.png";
"gdxgame-android/assets/packed/packed.png";
"/assets/packed/packed.png";
"assets/packed/packed.png";
"/packed/packed.png";
"packed/packed.png";
"/packed.png";
"packed.png";
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.toString();
//this caused shutdown of emulator
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.readString();
// I even tried setting the build path of the asset folder as source folder
// Also tried placing the image in the data folder.
// Tried using the absolute path too , i.e Gdx.files.absolute("the absolute");
// Tried passing the absolute path directly as string
Nothing seems to work for me.
The problem was very simple, the problem lied in the different versions of libgdx and the documentation. The answer explained below will solely confirm to libgdx version 0.9.8 only.
(Note: The usage of Texturepacker GUI was used to pack textures && not the method from the libgdx library)
First the "assetManager" had to be supplied with a file that contains the coordinates of the images which was a mistake here as i was supplying the packed image.
The GL20 should be able to parse NPOT images but it was unable to do so and the reason remains unknown so i had to pack the texture to a POT which was accomplished by selecting the POT options in the GUI. After doing this i was able to load the newly POT image easily with the following code
/**Mention only the folder/file under the asset dir**/
public class Gameconstants { public static final String location = "packed/packed.txt" }
/**access the same using the following command**/
private AssetManager assetManager;
assetManager.load(Gameconstants.location,Texture.class);
This Answer may not be very convincing but it surely solved my problem.
Thank you to all who helped :)
I am creating a text based program/ game and someone I know created one that opened the console into the Windows Command prompt. This made it so it could be continually refreshed, making the text clear and having a clean look, rather than the clunky console view, which just adds the text onto it, making a bad medium for a text-based game.
The following may give you an idea:
public static void main(String[] args) throws Exception {
// Since you are using eclipse, bin contains the compiled classes
// This depends on your working directory.
File directory = new File("bin");
// In my case, the main class is Game (in the default package)
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "java", "Game");
// directory defined above
pb.directory(directory);
// The fun begins!
pb.start();
}
The Game class containing only the following:
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println(sc.nextLine());
}
}
}
I have a requirement where i have to dynamically load java program based on the input. All java class files are placed in folder : C://Users/me/workspace/File/bin/Encrypt/.
there are 3 class files: Class1.class, Class2.class, Class3.class in this folder
To pick them up in runtime i am using below code:
package First;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class Main
{
public static void main(String[] args) {
// Say Class.class is the input file to be picked up.
String abc = "Class1.class";
try {
File file = new File("C:\\Users\\me\\workspace\\File\\bin\\Encrypt");
//convert the file to URL format
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
//load this folder into Class loader
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(abc);
System.out.println("cls.getName() = " + cls.getName());
cls.encrypt();
}
catch ( ClassNotFoundException | MalformedURLException e) {
e.printStackTrace();
}
}
}
I am facing below issues here:
The above code shows the error: .NoClassDefFoundError: Class1??
have used all types of - / ,\ ,//,\ in the path.
How can i call a method sum() in Class1 file ??
since you have ...\bin\Encrypt is the folder where you classes are i would bet the package for your classes is Encrypt. in this case abc should be
String abc = "Enrcypt.Class1";
if the classes are in the default package which means they have no package declaration than use
String abc = "Class1";
note also that there is no need for the extension .class in the name of the class.
To call the a method of class loaded this way you should refer to java refelction api.
first get a Method object from Class cl using getMethod
than use that method object's method invoke to call the function. you'll need to provide an instance of the Class dynamically loaded if the the method to call is not static, if it is static just pass null.