Encog Image Recognition, Invalid command Error - java

I am trying to run image recognition code in Encog framework.
This one
But I am having problems with the input. I am getting the following error
I am trying to get the picture into the program. I did it the following way.
public static void main(final String[] args) {
/*
if (args.length < 1) {
System.out
.println("Must specify command file. See source for format.");
} else {
*/
String string = "/Users/hehe/Downloads/Screenshot_2023-01-08_at_08.11.24-removebg-preview.png";
try {
final ImageNeuralNetwork program = new ImageNeuralNetwork();
program.execute(string);
} catch (final Exception e) {
e.printStackTrace();
}
Encog.getInstance().shutdown();
}
I commented first part of the code because I can't run this program in command line.
When I try to compile it like this
javac /Users/hehe/IdeaProjects/TestRencognition/src/main/java/ImageNeuralNetwork.java
I am getting error that the package org.encog does not exist

Related

Compile and run a Java program from another Java program

I am writing a program that takes the path to the input ".java" file with a main method. The program should then compile that file, and run it.
Let's say that the program I am trying to compile and run looks like this:
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
The program that performs compilation and tries to run it:
Evaluator.java
/**
* Matches any .java file.
*/
private static final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java");
private static String path;
/**
* Program entry point. Obtains the path to the .java file as a command line argument.
*
* #param args One argument from the command line: path to the .java file.
* #throws Exception
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
throw new IllegalArgumentException(
"Expected exactly one argument from the command line.");
}
if (!matcher.matches(Paths.get(args[0]))) {
throw new IllegalArgumentException(
String.format("File %s is not a valid java file.", args[0]));
}
// path is in a valid format
path = args[0];
// compile a program
compile();
// run a program
run();
}
/**
* Compiles a program.
*
* #throws Exception
*/
private static void compile() throws Exception {
System.out.println("Compiling the program ...");
Process p = Runtime.getRuntime().exec("javac " + path);
output("Std.In", p.getInputStream());
output("Std.Out", p.getErrorStream());
p.waitFor();
System.out.println("Program successfully compiled!\n");
}
/**
* Runs a program.
*
* #throws Exception
*/
private static void run() throws Exception {
System.out.println("Executing the program ...");
Process p = Runtime.getRuntime().exec("java " + getProgramName(path));
output("Std.In", p.getInputStream());
output("Std.Out", p.getErrorStream());
p.waitFor();
System.out.println("Program finished!");
}
private static void output(String stream, InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, CS));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(String.format("%s: %s", stream, line));
}
}
private static String getProgramName(String path) {
return path.replace(".java", "");
}
}
My "Main.java" file is located in the project root. I am running the program with a command line argument "./Main.java". Doing so, compiles the program correctly and yields a new file "Main.class". However, the run method outputs as follows:
Std.Out: Error: Could not find or load main class ..Main
What should be the problem here?
Try to set to java process you're launching the correct working directory and then set the related classpath.
This should help.
Update
I suggest to use the method Runtime.getRuntime().exec(String command, String[] envp, File dir).
Last parameter dir is the process working directory.
The Problem here is you are passing argument
./Main.java
instead, you should pass Main.java as an argument else you need to change your getProgramName() method to return the Class name correctly.
Which will let you compile the program perfectly with javac command but problem happens when you need to run the program because that command should be
java Main
whereas you are trying to execute
java ./Main

being able to read a file on command file using java

I have a program that works fine when it is run on eclipse (the program reads from a text file). However when it is complied and run on command line it can not find the text file I am reading from.
private void openfile()
{
try
{
file = new Scanner(new File("file.txt"));
}
catch(Exception e)
{
System.out.println("i hate command prompt");
}
private void readfile()
{
while(file.hasNext())
{
map_name = file.nextLine().split("\\s+");
}
}
private void closefile()
{
file.close();
}
can anyone explain how i can avoid this
You must place file.txt in the user.dir as specified by the File documentation. To determine what the user.dir is try printing out the property in your code, then placing the file in the directory.
System.out.println(System.getProperty("user.dir"));

Running Mac OSX commands from eclipse using java

I'm trying to make a simple java program to unhide the ~\Library\ folder on osx using terminal commands. As far as I have researched the code to run system commands from java is
Runtime.getRuntime().exec();
and is listed as such in every place I look it up.
However, my program doesn't work. Main method below.
public static void main(String[] args) throws IOException {
String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"};
try {
Runtime.getRuntime().exec(noHide);
System.out.println("library unhidden");
} catch (Exception e ) {
e.printStackTrace();
}
}
This program throws no exception, and compiles and executes fine, but the Library folder simply won't unhide. No matter what I reformat the cmd String. None of the formats below work
String noHide = "chflags nohidden ~/Library";
String[] noHide = {"chflags", "nohidden","~/Library"};
String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"};
If I remove the spaces they throw exceptions (well, not the String array objects). I can run the command (chflags noHidden ~/Library) absolutely fine from the osx terminal. Anyone have an idea why?
You need to use a try and catch, which you have. But, your main should be like this:
public static void main(String[] args) {
String[] noHide = {"chflags", "nohidden","~/Library"};
try {
Runtime.getRuntime().exec(noHide);
}
catch (Exception e) {
}
}
Basically, you don't need throws IOException. This worked for me, so if it still isn't working in your program, there may be a bigger problem with the way you have something set up.

How to call a class that accepts command line arguments?

I am not a good programmer. In school, I learned MATLAB. So i have no idea what I am doing.
I am working with the ThingMagic M6 reader. They have their own API. I wanted to create my own application to read the program. I want to use a sample program that they have supplied (since my program doesn't seem to work). However, the supplied program only accepts command line arguments. How do i change it so I can pass arguments to it in my code.
This is the supplied code: (at the command line I input tmr://10.0.0.101)
/**
* Sample program that reads tags for a fixed period of time (500ms)
* and prints the tags found.
*/
// Import the API
package samples;
import com.thingmagic.*;
public class read
{
static void usage()
{
System.out.printf("Usage: demo reader-uri <command> [args]\n" +
" (URI: 'tmr:///COM1' or 'tmr://astra-2100d3/' " +
"or 'tmr:///dev/ttyS0')\n\n" +
"Available commands:\n");
System.exit(1);
}
public static void setTrace(Reader r, String args[])
{
if (args[0].toLowerCase().equals("on"))
{
r.addTransportListener(r.simpleTransportListener);
}
}
static class TagReadListener implements ReadListener
{
public void tagRead(Reader r, TagReadData t) {
System.out.println("Tag Read " + t);
}
}
public static void main(String argv[])
{
System.out.println(argv.getClass().toString());
// Program setup
TagFilter target;
Reader r;
int nextarg;
boolean trace;
r = null;
target = null;
trace = false;
nextarg = 0;
if (argv.length < 1)
usage();
if (argv[nextarg].equals("-v"))
{
trace = true;
nextarg++;
System.out.println("Trace");
}
// Create Reader object, connecting to physical device
try
{
TagReadData[] tagReads;
r = Reader.create(argv[nextarg]);
if (trace)
{
setTrace(r, new String[] {"on"});
}
r.connect();
if (Reader.Region.UNSPEC == (Reader.Region)r.paramGet("/reader/region/id"))
{
r.paramSet("/reader/region/id", Reader.Region.NA);
}
r.addReadListener(new TagReadListener() );
// Read tags
tagReads = r.read(500);
// Print tag reads
for (TagReadData tr : tagReads)
System.out.println(tr.toString());
// Shut down reader
r.destroy();
}
catch (ReaderException re)
{
System.out.println("Reader Exception : " + re.getMessage());
}
catch (Exception re)
{
System.out.println("Exception : " + re.getMessage());
}
}
}
This is me trying to use it: (arg comes from a JTextField)
String[] argv = new String[1];
argv[0] = arg;
readOnceApp(argv);
I have a feeling there is a really simple answer to this problem, I just can't figure it out. I searched the internet for a few days and read books, and still can't figure it out. Any help is appreciated. Thank You.
edit: readOnceApp is one method I wrote. It is basically just the main method of the supplied code. I can include it, if it will help. I just didn't want to post too much code.
If you want to call the "main" method of a class from another class, do it like this:
String [] args = new String [1];
args[0]= "some param";
readOnceApp.main(args);
This is making the assumption that "readOnceApp" is the name of your class. (BTW, you should follow the convention of using capitalized class names, e.g. ReadOnceApp).
Hope this helps.

How to run compilr.com java .jar executable on windows when its not just java.lang* package

I'm starting to code in Java in spare work time. Problem is everything is locked down and I'm kinda new to ask IT department to install ide or javac at least to me(im not in IT) so Im using Compilr.com which is quite awesome. Yet I tried to save and run the Hello world code already precoded there:
public class ReadFile
{
public static void main(String args[])
{
System.out.println("Hello World from Compilr!");
System.out.println("Press any key to continue.");
try {
System.in.read();
} catch (Throwable t) {}
}
}
Then open windows cmd and run java -jar HelloWorld.jar Which Works.
Then I tried to build and run this code which throws the typical error that I havent properly setup classpath or some manifest made:
import java.io.*;
public class ReadFile{
public static void main(String[] args){
try {
FileReader input = new FileReader(args[0]);
BufferedReader bufRead = new BufferedReader(input);
String line;
int count = 0;
line = bufRead.readLine();
count++;
// Read through file one line at time. Print line # and line
while (line != null){
System.out.println(count+": "+line);
line = bufRead.readLine();
count++;
}
bufRead.close();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("Usage: java ReadFile filename\n");
}catch (IOException e){
// If another exception is generated, print a stack trace
e.printStackTrace();
}
}// end main
}
The thing it only generates a jar file so I dont have much of choice for compiling. How do I please make working code with all the available non-core java clasess?
/At home I get error even on the helloworld program: Error:Could not find or load main class Program.
You should be able to install both JDK with Netbeans and Eclipse in a local directory without admin rights. While it will be interesting to find out why compilr.com generated jar does not work for you for any serious work you will need a development environment.

Categories

Resources