Sorry for this noobie question, I'm new to Java, and instead of using IDE, i want to using command line to learn what's running under the hood
I'm following the Getting Started guild on MigLayout
#MigWindow.java
public class MigWindow {
public static void main(){
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());// a simple line to make sure the library jar import correctly
}
}
and compile with these command:
javac -cp ./MigLayout.jar MigWindow.java
and I got a error:
MigWindow.java:3: cannot find symbol
symbol : class MigLayout
location: class MigWindow
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());
^
1 error
It seems the jar library doesn't import correctly, any idea?
~
Make sure you add the import for MigLayout
import net.miginfocom.swing.MigLayout;
It may sound obvious, but make sure MigLayout.jar the current directory when calling javac here and that your JAR file has not been corrupted.
Update:
To check that your JAR file does contain the class you can do:
jar tvf MigLayout.jar
and check for the MigLayout class. Failing to find the class you can download the correct one from here.
You are missing an import statement in your Source File. The compiler does not know where 'MigLayout' is coming from.
Add at the top of your file, but below of your package statement (if any) an import, e.g.
import package.MigLayout;
This tells the compiler what to import from the given class path. You will need to replace package with the correct package.
Related
To start things off, I am entirely new to Java. I'm a C#/Powershell guy. A client at my IT Firm had an issue with a java program that they were executing on a daily basis that was having issues. According to Windows, the original program was written in April of 2011. I was able to unzip the file and pulled out all of the java files. I then rebuilt the program's structure in NetBeans and am getting ready to start editing. However, each *Test.java file is unable to import junit.framework.TestCase. In the original program file, each of these files were in the same folders as their associated files. From what I can tell, that is not best practices but it was the folder structure I found in the *.jar file I pulled them from. i.e.:
+ Source Packages
|
+--+ Folder
|
+--Example.java
|
+--ExampleTest.java
This leads me to 2 potential issues:
Reading similar threads regarding junit.framework "does not exist", there is mention of adding the junit.jar to the POM or adding the dependency to maven. For NetBeans, how do I do this? Using the "Add Dependency" menu, I am unable to find a "junit.framework" and there is 125,000 results for junit that I am unsure which one I need. Any insights? At the time of the original program's writing, v3 and v4 were both released, although v3.8.1 remained in use for some time beyond the adoption of v4.
For its use-case, see below. I assume all the errors are related to the junit import, so I included them as comments.
package com.example.program;
import java.util.Properties;
import junit.framework.TestCase;
/* Import files specific to program */
public class ExampleTest extends TestCase { //Cannot find symbol (class) "TestCase"
private Properties config = null;
#Override //Error: method does not override or implement a method from a supertype
/* SetUp function/method w/out any issues, creates config Properties object */
public void testExample(){
String line = "*"; // some csv line being parsed
CSVLine csvLine = new CSVLine(line, config);
assertEquals(/* does stuff */); // Error: cannot find "symbol" (method) "assertEquals"
assertTrue(/* does stuff */); // Error: cannot find "symbol" (method) "assertTrue"
assertTrue(/* does stuff*/); // Error: cannot find "symbol" (method) "assertTrue"
}
}
Do I need to move these Test.java files into a folder under the Test Packages section of the POM? Why would the original program have them in the same directory as their counterparts? Does some aspect of compiling/building move them to the same location?
The purpose of my project is to create PNG pictures from SVG using specific fonts (I have tried numerous converter services on the web but none of them worked with external fonts, and the JAVA program I have developed 10 years ago does no longer work).
My actual problem is the import of the batik.transcoder components into my program. Currently, it looks as follows:
<code>
// is this the really the parent of JPEGTranscoder / PNGTranscoder??
import javax.imageio.ImageTranscoder;
// Neither org.apache.batik... nor batik... is correct !!??
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import batik.transcoder.TranscoderException;
import batik.transcoder.TranscoderInput;
import batik.transcoder.TranscoderOutput;
import batik.transcoder.PNGTranscoder;
import batik.transcoder.image.JPEGTranscoder;<br>
// or did I import the wrong jars ??
// (taken from batik-1.14 and afterwards from batik-1.7; neither did work!)
public class SVGConverter {
...
// all code lines below return the same error: "... cannot be resolved to a type"
TranscoderInput input = new TranscoderInput(svgURI);
TranscoderOutput output = new TranscoderOutput(ostream);
ImageTranscoder t;
...
t = new JPEGTranscoder();
...
t = new PNGTranscoder();
...
}
</code>
First I tried to import the batik components as modules on the module path but without success. In a contribution to stackoverflow I then found the hint to put the batik components not onto the module path but to the class path (Has batik not been converted into modules?). However, no success either.
I have copied all jar's contained in batik-1.14.zip and later of batik-1.7.zip into the lib directory within the project. No effect whatsoever...
I was using "import org.apache.batik.transcoder.XXX" in my previous program in 2013: Doesn't work any more. But using "import batik.transcoder.XXX" doesn't work either.
Thus, the following questions:
1. Is it correct that batik classes/jars have to be used via the class path?
2. What is the correct package name to address the respective packages and classes?
3. Is it correct to load the corresponding jar's into the lib directory of the project?
I am trying to compile a small program. During compilation, it tries to look at wrong path for java util libs. Below is the compilation output
javac -Xlint:unchecked adssim/util/XMLParser.java
adssim/util/XMLParser.java:3: error: cannot access util
import java.util.Vector;
^
bad source file: ./java/util.java
file does not contain class java.util
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
File loation
/apps/oasis/adarsh/adssim/util>ls -lrt XMLParser.java
-rwxrwxrwx 1 oasis oasis 1258 Apr 8 15:49 XMLParser.java
Classpath
/apps/oasis/adarsh/adssim/util>echo $CLASSPATH
:/apps/oasis/java/classes12.jar:/apps/oasis/java/ifxjdbc.jar:/apps/oasis/java/log4j-1.2.14.jar:/apps/oasis/java/commons-io-2.5.jar:/apps/oasis/adarsh/adssim:/apps/oasis/adarsh/adssim/util:/apps/oasis/adarsh/adssim/util/crypto:/apps/oasis/adarsh/adssim/channel:/apps/oasis/adarsh
File Source
package adssim.util;
import java.util.Vector;
public class XMLParser
{
public static Vector getXMLTagValue(String xml, String section) throws Exception
{
String xmlString = new String(xml);
Vector v = new Vector();
What am i doing wrong guys ?
Is there a file on your disk that is actually called util.java in a directory called java? Maybe you have created such a file by mistake?
This part of the error message makes it sound like that:
bad source file: ./java/util.java
I have the following file structure in my eclipse workspace
core
src
com.mygame.game (package)
Ability.java
Game.java
...
Allies-Green.java
Ability.java and Game.java were created using the 'right-click on package->New->Class' method. Allies-Green.java, however, was created using the 'right-click on package->New->File' method. Each time I load my workspace, all code that references Allies-Green.java is marked as an error. If I open Allies-Green.java though, and edit one character and resave it, the problem goes away. Does anyone know what could be causing this issue? Here is Allies-Green.java relevant code for reference:
package com.mygame.game;
import java.util.ArrayList;
import ...
//only class in file
class AllyGreen extends Ally {
...
}
I want to learn to write my own packages so I'm not also relient on an IDE, which I feel I have became. The problem is I cannot figure out how to run my own package, or what the proper method is to run your own package.
Here's a resource I used to learn some basics: http://javaworkshop.sourceforge.net/chapter3.html
Here's my current file structure:
Main.java
/src
projectaqua/
GameFrame.java
/classes
projectaqua/
GameFrame.class
I ran the command in the root directory of the project:javac -d ./classes/ ./src/projectaqua/*.java
I originally created a Main file in the /src/projectaqua directory and attempted to run the file. I was given this error:
Main.java:1: error: package projectaqua does not exist
import projectaqua.GameFrame;
I tried running the application in the /classes/projectaqua directory when compiling the Main file with the package, which gave me a class not defined error.
This compiled my package, the problem I'm facing is I don't understand how you are supposed to import your own package to run it, and where would the file to run the package be?
From what I've learned in school, when writing a GUI application we create a class that has a main function in it to instantiate the frame, and that's it's only job. Where would this be in this structure?
Intuitively it seems that file would be outside of the src files, but I feel like that removes the purpose of the src files. I haven't found anything useful on stackoverflow to this topic, if you do or have please point me in that direction.
More source code:
GameFrame Class:
package projectaqua;
import javax.swing.JFrame;
public class GameFrame extends JFrame
{
private int WINDOW_HEIGHT = 500;
private int WINDOW_WIDTH = 500;
private String title = "Project Aqua";
private boolean isVisible = true;
public GameFrame()
{
// Basic Window Defaults
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle(this.title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Content Pane junk
// Will be added
setVisible(this.isVisible);
}
}
The Main class
import projectaqua.GameFrame;
public class Main
{
public static void main(String[] args)
{
GameFrame launch = new GameFrame();
}
}
I now see your problem.
In your question you were not clear that you had trouble running v. compiling. Had you posted this error trace it would have been immediately clear to me what your problem is:
unrollme-dev-dan:projectaqua Dan$ java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: projectaqua/Main)
Also note that had you Googled NoClassDefFoundError would have found this. The moral here is: understand and research your exact error.
Anyway
unrollme-dev-dan:classes java projectaqua/Main
is what you want. Notice the change of directory. I never bothered to understand why, has to do with relationship between package hierarchy and file structure hierarchy.
Java had two choices when designed: Assume the thing you are talking about is in the global package (yuck!) or try to guess what package it is in. It treats any folder below your working directory as packages. So even though it found a Main class in the directory from which you were running it did not find a Main class in the namespace corresponding to the directory . i.e. the global one.
When you run from one directory up and tell it to run something in projectaqua/ it is now looking for classes starting with projectaqua.
Alternately if you run
unrollme-dev-dan:projectaqua java projectaqua.Main
It looks for the right thing.
try this command at the root of your project
javac -cp ./classes -d ./classes ./src/projectaqua/*.java
Also make sure both your Main.java and GameFrame.java has package projectaqua; at the beginning