I have tried many methods over the last few days with no success...I've trawled through various Stackoverflow entries and to no avail...I must be missing something.
I have tried across three different IDEs...IntelliJ, Eclispe and Netbeans.
The problem is when trying to turn my program into an executable jar it is unable to run (either by double clicking or running through command).
When executing the following on command:
java -jar D:\Computing\Programming\Java\Projects\JavaFXGameMenu\out\artifacts\JavaFXGameMenu_jar\JavaFXGameMenu.jar
I get: Error: Could not find or load main class root.Main
When i run the same but with javaw instead.. i get not error message, but nothing happens either.
I am predominately using IntelliJ as its a JavaFX application that I am building.
This is the project hierarchy:
When creating the Artifact I choose the following based upon other threads:
I then re run this using: Java -jar D:\Computing\Executables\JavaFXGameMenu.jar
I get the following issue:
I have put the relevant environment variables into my system and Path and I am using jre1.8.0_144.
Any help with tracking down what the problem could be is greatly appreciated
Code below...but this fully compiles and runs within IDE without errors...the problem is when its turned into a .jar and ran from command.
package root;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main extends Application {
private double width = 1920;
private double height = 1080;
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(width, height); //W:860 H:600
ImageView imgLogo = null;
ImageView bottomlogo = null;
MenuItem newGame = new MenuItem("NEW GAME");
MenuItem continueGame = new MenuItem("CONTINUE");
MenuItem friends = new MenuItem("FRIENDS");
MenuItem settings = new MenuItem("SETTINGS");
MenuItem store = new MenuItem("STORE");
MenuItem exit = new MenuItem("EXIT");
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))) {
ImageView img = new ImageView(new Image(is));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/logo.png"))) {
imgLogo = new ImageView(new Image(is));
imgLogo.setX(1000);
imgLogo.setY(100);
imgLogo.setFitWidth(600);
imgLogo.setFitHeight(300);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/SteamAgony.png"))) {
bottomlogo = new ImageView(new Image(is));
bottomlogo.setX(100);
bottomlogo.setY(800);
bottomlogo.setFitHeight(200);
bottomlogo.setFitWidth(200);
bottomlogo.setOpacity(0.7);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
MenuBox menu = new MenuBox(
newGame,
continueGame,
friends,
settings,
store,
exit);
menu.setTranslateX(width / 3.4);
menu.setTranslateY(height / 2.5);
settings.setOnMouseClicked(event -> new SceneCreator().createScene(200,300));
exit.setOnMouseClicked( event -> Platform.exit());
root.getChildren().addAll(menu, imgLogo, bottomlogo);
return root;
}
#Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
#Override
public void stop(){
//TODO
}
public static void main(String[] args) {
launch(args);
}
}
The error is at Main.createContent line 102. Maybe you forgot to initialize the child node (I'm not really familiar with JavaFX).
As #cedrikk mentioned, the problem is related to your code in Main.createContent.
You said the problem is when trying to run the jar as an executable -
did you try to run it within the IDE? If not - you should try to, and while at it - debug it to help you find the problem. Just right click your Main class and choose debug.
Regarding running it with javaw, the reason you got no error messages is because javaw executes java programs without a console - where you would normally get error messages unless using some logging solution.
You are adding a null element as a child of a Pane, causing the null pointer issue you are facing, if you use the debugger you'll be able to find where the variable that shouldn't be null is being set to null.
Edit - after code added
You are seeing 2 fields in try catches (imageLogo, bottomLogo), and adding them even if they fail, which adds nulls, causing the error.
You use a relative path for the images, this is probably the issue, are you running the jar from the project root? If not you could put the absolute path, but using resources would be more reliable, and allow the jar to run on different computers.
The problem was down to declaring inputStreams.
This works for both in IDE and running from jar:
String bgImg = "root/resources/dark.jpg";
URL bgImgPath = Main.class.getClassLoader().getResource(bgImg);
ImageView img = new ImageView(new Image(bgImgPath.toExternalForm()));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
Compared to what I had before:
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))){
ImageView img = new ImageView(new Image(is));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
}catch (IOException e) {
System.out.println("Could not load");
}
The changes to the paths, were from where I tried adding the resource folder to the root folder instead of within src.
Related
I'm creating a PhpStorm plugin with IntelliJ IDEA Community Edition and I would like to know how to create a file on disk from a PSIFile or VirtualFile.
Here my code: (The context is an action from NewGroup)
I've tried to use the PsiDirectory.copyFileFrom method to create the file but I have an exception com.intellij.util.IncorrectOperationException: Cannot copy non-physical file: PHP file
package fr.florent.idea.zendgenerator.action.NewGroup;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.util.ResourceUtil;
import com.jetbrains.php.lang.PhpFileType;
import fr.florent.idea.zendgenerator.action.AbstractDumbAwareAction;
import icons.PhpIcons;
import org.jetbrains.annotations.NotNull;
import java.net.URL;
public class CreateQueryAction extends AbstractDumbAwareAction {
public CreateQueryAction() {
super("Query", "Create query", PhpIcons.Php_icon);
}
#Override
public void actionPerformed(#NotNull AnActionEvent e) {
URL url = ResourceUtil.getResource(getClass(), "templates", "query.php"); // it contains an empty PHP class
VirtualFile virtualFile = VfsUtil.findFileByURL(url);
PsiFile file = PsiFileFactory.getInstance(e.getProject()).createFileFromText(
virtualFile.getPath(),
PhpFileType.INSTANCE,
LoadTextUtil.loadText(virtualFile)
);
PsiDirectory directory = LangDataKeys.IDE_VIEW.getData(e.getDataContext()).getOrChooseDirectory();
directory.copyFileFrom("query.php", file);
System.out.println("Create query");
}
}
I would like to have the file created in the project folder from the context of my action.
It will be great if someone can explain the process of creating a file in a IntelliJ plugin. I think the docs is really light.
In my case I would like to have the process to edit the query.php file, rename the class name, add method, properties, doc block and save it to the disk but I don't understand the PSI element.
You might want to ask this also on JB forum, there's a similar question: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001787320-Create-VirtualFile-or-PsiFile-from-content
The answer is:
final PsiFileFactory factory = PsiFileFactory.getInstance(project);
final PsiFile file = factory.createFileFromText(language, text);
I want to create a RapidMiner classifier on Java that classifies a user based on his/her touch input. I have searched for days now, I have RapidMiner Studio and have downloaded the rapidMiner-studio from github - but I don't know which one should I include on my library/build path.
Do I just include all of them? Any help would be greatly appreciated.
I found the answer to my question, so far I found out that you can run a process ( a .rmp) file you created from RapidMiner in Eclipse, and the JAR files i added are located at C:\Program Files\RapidMiner\RapidMiner Studio\lib
Here's my code:
package rapid_process;
import com.rapidminer.Process;
import com.rapidminer.RapidMiner;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorException;
//import com.rapidminer.operator.io.ExcelExampleSource;
import com.rapidminer.tools.XMLException;
import java.io.File;
import java.io.IOException;
import java.lang.Object;
public class process {
public static void main(String[] args) throws IOException, XMLException, OperatorException {
/*// Path to process-definition
final String processPath = "C:/Users/Evie/.RapidMiner/repositories/Local Repository/processes/Compare ROCs.rmp";
// Init RapidMiner
RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
RapidMiner.init();
// Load process
final com.rapidminer.Process process = new com.rapidminer.Process(new File(processPath));
process.run();*/
try {
RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
RapidMiner.init();
Process process = new Process(new File("C:/Users/YourUser/.RapidMiner/repositories/Local Repository/processes/Compare ROCs.rmp"));
process.run();
} catch (IOException | XMLException | OperatorException ex) {
ex.printStackTrace();
}
}
}
I've made a simple dictionary using JavaFX. I've used a SQLite Database and some pictures into my application. I exported the application as a runnable JAR file using e(fx) Eclipse. I followed the steps described here - https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1.
After exporting I opened the .JAR file. It opened successfully but wasn't working properly. It wasn't showing the results from the database and images.
When I ran the application into Eclipse workspace before building & exporting, it worked fine.
Where is the problem? How do I fix it?
Here is the code for the Controller Class function:
package imran.jfx.application;
import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class Application_Controler implements Initializable{
#FXML
private HBox hBox;
#FXML
private Label searchLabel;
#FXML
private TextField searchWord;
#FXML
private VBox vBox;
#FXML
private Text BanglaMeaning;
#FXML
private TextArea bnMeaningTxt;
#FXML
private Text bAcaMeaning;
#FXML
private ScrollPane bAcaMeaningImg;
#FXML
private Label footerLabel;
ResultSet result;
PreparedStatement doQuery;
Connection conn;
String query;
#Override
public void initialize(URL location, ResourceBundle resources) {
try
{
Class.forName("org.sqlite.JDBC");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url="jdbc:sqlite:src/imran/ankurdb/meaning/bn_words.db";
try
{
conn = DriverManager.getConnection(url);
}
catch (SQLException e) {
e.printStackTrace();
}
}
#FXML
void showMeaning(ActionEvent event) throws Exception {
bnMeaningTxt.clear();
String text=searchWord.getText();
query = "select en_word,bn_word from words where en_word='"+text+"'";
doQuery = conn.prepareStatement(query);
result = doQuery.executeQuery();
int i=0;
while (result.next())
{
if(i==0)
{
bnMeaningTxt.appendText(result.getString(1) + "\t\t" + result.getString(2));
i++;
}
else
bnMeaningTxt.appendText(" , "+result.getString(2));
}
File file = new File("src/imran/bnacademy/meaning/"+text);
Image image = new Image(file.toURI().toString());
bAcaMeaningImg.setContent(new ImageView(image));
}
}
There are multiple problems here.
You are trying to create a database file inside your project. This is not going to work when you have packaged your project as a jar. You should give it a url outside your project. The database files should be placed always outside the project, so that when you package your project as a jar, they can still be created, read and written to.
If the images are present inside your jar, you cannot use io.File to load images. You need to use the class loader to load them instead.
Code
URL url = getClass().getResource("/imran/bnacademy/meaning/" + text);
Image image = new Image(url.toExternalForm());
I have images of codes that I want to decode. How can I use zxing so that I specify the image location and get the decoded text back, and in case the decoding fails (it will for some images, that's the project), it gives me an error.
How can I setup zxing on my Windows machine? I downloaded the jar file, but I don't know where to start. I understand I'll have to create a code to read the image and supply it to the library reader method, but a guide how to do that would be very helpful.
I was able to do it. Downloaded the source and added the following code. Bit rustic, but gets the work done.
import com.google.zxing.NotFoundException;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Reader;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.LuminanceSource;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.*;
import com.google.zxing.qrcode.QRCodeReader;
class qr
{
public static void main(String args[])
{
Reader xReader = new QRCodeReader();
BufferedImage dest = null;
try
{
dest = ImageIO.read(new File(args[0]));
}
catch(IOException e)
{
System.out.println("Cannot load input image");
}
LuminanceSource source = new BufferedImageLuminanceSource(dest);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
barcodeFormats.add(BarcodeFormat.QR_CODE);
HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>(3);
decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = null;
try
{
result = xReader.decode(bitmap, decodeHints);
System.out.println("Code Decoded");
String text = result.getText();
System.out.println(text);
}
catch(NotFoundException e)
{
System.out.println("Decoding Failed");
}
catch(ChecksumException e)
{
System.out.println("Checksum error");
}
catch(FormatException e)
{
System.out.println("Wrong format");
}
}
}
The project includes a class called CommandLineRunner which you can simply call from the command line. You can also look at its source to see how it works and reuse it.
There is nothing to install or set up. It's a library. Typically you don't download the jar but declare it as a dependency in your Maven-based project.
If you just want to send an image to decode, use http://zxing.org/w/decode.jspx
I have designed a UI in javafx scene builder, which has a simple button in stackpane.And I have named the controller class as simplecclass. I have saved the fxml as simple.fxml.
I have created a controller class in netbeans, which simply prints some msg on clicking the button.
In the NewFXBuilder java , I have loaded simple.fxml. Please find below the NewFXBuilder.java code.
package javafxapplication2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
public class NewFXbuilder extends Application {
#Override
public void start(Stage primaryStage) {
try {
StackPane page = (StackPane) FXMLLoader.load(NewFXbuilder.class.getResource("simple.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("FXML is Simple");
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(NewFXbuilder.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
Application.launch(NewFXbuilder.class, (java.lang.String[])null);
}
}
My simple.fxml,simplecclass.java and NewFXbuilder.java all resides in the same folder javafxapplication2.
while running NewFXBuilder.java, but it gives me the following error.
javafxapplication2.NewFXbuilder start
SEVERE: null
javafx.fxml.LoadException: java.lang.ClassNotFoundException: simplecclass
javafxapplication2.NewFXbuilder start SEVERE: null
javafx.fxml.LoadException: java.lang.ClassNotFoundException:
simplecclass
Looks like a problem in the FXML file. Make sure you import simplecclass in the FXML file.
The mistake I did was forgotten to add java packagename in the controller class name field in scene builder. It should have been packagename.simplecclass but I gave simplecclass alone,which is a mistake.